How to get a specific embedded document inside a MongoDB collection?
mongodb
Solution
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.
Problem
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.