MongoDb Query returning unwanted documents
up vote
0
down vote
favorite
I have a database containing documents of two structures:
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
and
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:
{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}
returns all documents (even where "bla1.name" is an array) containing the name: 'something'.
What am I doing wrong?
mongodb mongodb-query
add a comment |
up vote
0
down vote
favorite
I have a database containing documents of two structures:
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
and
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:
{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}
returns all documents (even where "bla1.name" is an array) containing the name: 'something'.
What am I doing wrong?
mongodb mongodb-query
Why need to checktype
? Simplydb.collection.find({ "bla1.name": "something" })
should work.
– Anthony Winzlet
Nov 8 at 10:57
@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02
@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03
@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a database containing documents of two structures:
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
and
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:
{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}
returns all documents (even where "bla1.name" is an array) containing the name: 'something'.
What am I doing wrong?
mongodb mongodb-query
I have a database containing documents of two structures:
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": "",
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
and
{
"name": "",
"name_ar": "",
"description": "",
"bla1": {
"name": ,
"link": "",
"Logo": ""
},
"bla2": {
"name": "",
"id": ""
}
}
I want to query my collection to get documents with "bla1.name" exactly equal to something. However using the following query:
{$and: [{'bla1.name': {'$type': 'string'}}, {"bla1.name":'something'}]}
returns all documents (even where "bla1.name" is an array) containing the name: 'something'.
What am I doing wrong?
mongodb mongodb-query
mongodb mongodb-query
edited Nov 8 at 11:20
mbuechmann
2,44011123
2,44011123
asked Nov 8 at 10:36
Charbel Hanna
156
156
Why need to checktype
? Simplydb.collection.find({ "bla1.name": "something" })
should work.
– Anthony Winzlet
Nov 8 at 10:57
@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02
@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03
@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11
add a comment |
Why need to checktype
? Simplydb.collection.find({ "bla1.name": "something" })
should work.
– Anthony Winzlet
Nov 8 at 10:57
@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02
@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03
@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11
Why need to check
type
? Simply db.collection.find({ "bla1.name": "something" })
should work.– Anthony Winzlet
Nov 8 at 10:57
Why need to check
type
? Simply db.collection.find({ "bla1.name": "something" })
should work.– Anthony Winzlet
Nov 8 at 10:57
@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02
@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02
@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03
@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03
@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11
@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
From the MongoDB docs:
$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.
That means: If an array has at least one element with the given type it gets selected.
If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:
$and: [
// not necessary any more, as this selection is already implied by the last part
// {
// "bla1.name": {
// "$type": "string"
// }
// },
{
"bla1.name": {
$not: {
"$type": "array"
}
}
}, {
"bla1.name": "something"
}
]
See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior
Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
From the MongoDB docs:
$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.
That means: If an array has at least one element with the given type it gets selected.
If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:
$and: [
// not necessary any more, as this selection is already implied by the last part
// {
// "bla1.name": {
// "$type": "string"
// }
// },
{
"bla1.name": {
$not: {
"$type": "array"
}
}
}, {
"bla1.name": "something"
}
]
See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior
Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8
add a comment |
up vote
2
down vote
accepted
From the MongoDB docs:
$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.
That means: If an array has at least one element with the given type it gets selected.
If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:
$and: [
// not necessary any more, as this selection is already implied by the last part
// {
// "bla1.name": {
// "$type": "string"
// }
// },
{
"bla1.name": {
$not: {
"$type": "array"
}
}
}, {
"bla1.name": "something"
}
]
See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior
Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
From the MongoDB docs:
$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.
That means: If an array has at least one element with the given type it gets selected.
If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:
$and: [
// not necessary any more, as this selection is already implied by the last part
// {
// "bla1.name": {
// "$type": "string"
// }
// },
{
"bla1.name": {
$not: {
"$type": "array"
}
}
}, {
"bla1.name": "something"
}
]
See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior
Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8
From the MongoDB docs:
$type now works with arrays in the same way it works with other BSON types. Previous versions only matched documents where the field contained a nested array.
That means: If an array has at least one element with the given type it gets selected.
If you want to exclude arrays as type you have to extend your query. As the query already matches strings, you can exclude the type selection for string:
$and: [
// not necessary any more, as this selection is already implied by the last part
// {
// "bla1.name": {
// "$type": "string"
// }
// },
{
"bla1.name": {
$not: {
"$type": "array"
}
}
}, {
"bla1.name": "something"
}
]
See the official docs: https://docs.mongodb.com/manual/reference/operator/query/type/#behavior
Here is a working demo on the Mongo playground: https://mongoplayground.net/p/3ri7Bjfrae8
edited Nov 8 at 11:06
answered Nov 8 at 10:59
mbuechmann
2,44011123
2,44011123
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205956%2fmongodb-query-returning-unwanted-documents%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Why need to check
type
? Simplydb.collection.find({ "bla1.name": "something" })
should work.– Anthony Winzlet
Nov 8 at 10:57
@AnthonyWinzlet that does not work as arrays containing the given value get selected, too.
– mbuechmann
Nov 8 at 11:02
@AnthonyWinzlet that doesnt work if the array contains the name im looking for
– Charbel Hanna
Nov 8 at 11:03
@mbuechmann Oh yes!!! I had mistaken.
– Anthony Winzlet
Nov 8 at 11:11