I've been going through a bunch of the mongodb docs and questions on Stackoverflow but I can't seem to find the answer I'm after.
A document in my data in MongoDB looks a bit like this:
{
"_id" : ObjectId("592eec62f74887609678cb55"),
"name" : "whatever",
"arrayOfObjects" : [
{
"refersToSomethingElse" : "somethingElse1",
"isHandled" : false
},
{
"refersToSomethingElse" : "somethingElse2",
"isHandled" : true
}
]}
I'm trying to get all documents where in one and the same nested object refersToSomethingElse = "somethingElse1"
When running the next query, I don't want to get the object I wrote above, but MongoDB is returning it anyway:
db.getCollection('data').find({
"name" : "whatever",
"arrayOfObjects.refersToSomethingElse" : "somethingElse1",
"arrayOfObjects.isHandled" : true
});
This means the 2 last parts of the query aren't being applied to the same nested object, which is what I'm trying to achieve.
I guess I'm trying to find something like the positional operator $, but in a find query.
EDIT: Got the result I was looking after using this query:
db.getCollection('webpages').find({
arrayOfObjects: {$elemMatch: {
refersToSomethingElse: "somethingElse1",
isHandled: false
}}
});