How to get a specific embedded document inside a MongoDB collection? [duplicate]

I have a collection Notebook which has embedded array document called Notes. The sample

document looks like as shown below.

{
"_id" : ObjectId("4f7ee46e08403d063ab0b4f9"),
"name" : "MongoDB",
"notes" : [
            {
              "title" : "Hello MongoDB",
              "content" : "Hello MongoDB"
            },
            {
              "title" : "ReplicaSet MongoDB",
              "content" : "ReplicaSet MongoDB"
            }
         ]
}

I want to find out only note which has title "Hello MongoDB". I am not getting what should

be the query. Can anyone help me.


Solution 1:

You can do this with mongo version higher 2.2

the query like this:

db.coll.find({ 'notes.title': 'Hello MongoDB' }, {'notes.$': 1});

you can try with $elemMatch like Justin Jenkins

Solution 2:

Outdated answer: See the other answers.


I don't believe what you are asking is possible, at least without some map-reduce maybe?

See here: Filtering embedded documents in MongoDB

That answer suggests you change your schema, to better suit how you'd like to work with the data.

You can use a either "dot notation" or $elemMatch to get back the correct, document that has the matching "note title" ...

> db.collection.find({ "notes.title" : "Hello MongoDB"}, { "notes.title" : 1"});

or ...

> db.collection.find({ "notes" : { "$elemMatch" : { "title" : "Hello MongoDB"} }});

But you will get back the whole array, not just the array element that caused the match.

Also, something to think about ... with your current setup it woud be hard to do any operations on the items in the array.

If you don't change your schema (as the answer linked to suggests) ... I would consider adding "ids" to each element in the array so you can do things like delete it easily if needed.

Solution 3:

You can do this in MongoDb version 3.2+ with aggregation.

Query:

db.Notebook.aggregate(
    {
        $project: {
            "notes": {
                $filter: {
                    input: "$notes",
                    as: "note",
                    cond: { 
                        $eq: [ "$$note.title", "Hello MongoDB" ]
                    }
                }
            }
        }
    }
)

Result:

{ 
    "_id" : ObjectId("4f7ee46e08403d063ab0b4f9"), 
    "notes" : [ 
        { 
            "title" : "Hello MongoDB", 
            "content" : "Hello MongoDB" 
        } 
    ] 
}

$$ used here to access the variable. I used here to access the newly created note variable inside the $filter.

You can find additional details in the official documentation about $filter, $eq and $$.

$filter: Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.

$eq: Compares two values and returns true/false when the values are equivalent or not (...).

$$: Variables can hold any BSON type data. To access the value of the variable, use a string with the variable name prefixed with double dollar signs ($$).


Note:

Justin Jenkin's answer is outdated and kop's answer here doesn't return multiple documents from the collection. With this aggregation query, you can return multiple documents if needed.

I needed this and wanted to post to help someone.