In my mongodb collection, I have a collection with two levels of embedded documents.
Collection
- field1
- field2
- EmbeddedDocuments1
- field_a
- field_b
- EmbeddedDocument_a
- field_A
- field_B
- field_C
- EmbeddedDocuments2
- field_c
- field_d
- EmbeddedDocument_a
- field_D
- field_E
- field_F
- EmbeddedDocuments3
- field_e
- field_f
- EmbeddedDocument_a
- field_G
- field_H
- field_I
When I do a query to find a specific 2nd level document it takes a long time, ~= 500ms
The query I tried is something similar to the line below, which I only want to get the data from a 2nd level document.
db.collections.find({ "embedded_documents_1.embedded_documents_2._id":ObjectId("502e8f5565ce10780f00000c") })
However, this returns the entire one collection, which contains field1, field2, all EmbeddedDocuments
Am I doing something wrong here?
Your query is searching for any document that matches “embedded_documents_1.embedded_documents_2._id”:ObjectId(“502e8f5565ce10780f00000c”)
So it has returned in full each document that matches this. Embedded documents are by nature, documents within another document. So mongodb has returned the full document.
You need to limit the values returned to only the second level document. Have a look http://www.mongodb.org/display/DOCS/Querying#Querying-FieldSelection for information on how to limit your query.