Retrieving an embedded document from the a mongo document?
-
i had a mongo document structured as { "id": 1, "userId": 1, "layout": 1, "snapshotDetails"â¼: { "0": { "id": 1, "name": "jaison", "type": "justus", "width": 100, "height": 100, "position": 1 }, "1": { "id": 2, "name": "jatin", "type": "justus", "width": 100, "height": 100, "position": 2 } }, "_id": ObjectId("4f58932309ac38f808000002") } and i need to extract the particular embedded document "1": { "id": 2, "name": "jatin", "type": "justus", "width": 100, "height": 100, "position": 2 } under snapshotDetails. for that i build a query like db.MasterDashboard.find({ "userId" : 1, "http://snapshotDetails.id" : 1 }, { "snapshotDetails" : 1 }); But i am not getting the output properly. the output of the query is [ "0": { "id": 1, "name": "jaison", "type": "justus", "width": 100, "height": 100, "position": 1 }, "1": { "id": 2, "name": "jatin", "type": "justus", "width": 100, "height": 100, "position": 2 } ] is any issue in my query. please help
-
Answer:
By default, MongoDB queries are designed to query entire http://documents.So when you run a find on this: { "userId" : 1, "http://snapshotDetails.id" : 1 } You are actually saying this: "find all documents with userId equal to 1 and having a snapshotDetail Id equal to 1" Now you also request a special return value: { "snapshotDetails" : 1 } And that's what you are getting back. You are getting the whole snapshotDetails field. What you actually want is the just the snapshotDetails that matched. Unfortunately, there is no way to do this. You can look at the docs here, you will see that feature simply does not exist. http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields You are not completely out of luck. If you take a look at the new Aggregation Framework, this has the features you need. http://www.mongodb.org/display/DOCS/Aggregation+Framework You should be able to do this with project and match.
Gaëtan Voyer-Perrault at Quora Visit the source
Other answers
If you use a ODM framework like Mongoose, you can do the query that Gaetan is suggesting and then use a helper function that the Mongoose framework provides with its DocumentArrays to retrieve a particular embedded document by id.
Thomas Chen
db.collectionname.aggregate([ // Match the documents by query { "$match": { "id" : "1", "snapshotDetails.1."id": { "$in": [ 2 ] }, }}, // De-normalize nested array { "$unwind": "$snapshotDetails" }, { "$unwind": "$snapshotDetails.1" }, // Filter the actual array elements as desired { "$match": { snapshotDetails.1."id": { "$in": [ 2 ] }, }},])
Shibi M
Related Q & A:
- How to replace a string inside of a Word Document?Best solution by Stack Overflow
- What are the duties of a senior document controller?Best solution by Yahoo! Answers
- How do I audit a policy document? What areas should I look at and how should it be written?Best solution by Yahoo! Answers
- How do you fax a simple document?Best solution by Stack Overflow
- How do I attach a word document to an email?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.