1

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
    }}
});
Adriaan Marain
  • 186
  • 1
  • 10
  • Thanks, the question you linked wasn't the same as mine because I'm trying to get the entire document, but I found my answer in the comments there. I had to use the $elemMatch operator. – Adriaan Marain Jun 01 '17 at 11:27

0 Answers0