Can only one subdocument be returned?
Yes, but not the way you want. If you do the following, you will get only the first element of the array:
coll.find({_id:'2'}, { 'objects.0': 1})
However, what you really want is what looks like this:
coll.find({_id:'2', 'objects._id': '3'}, { 'objects.$' : 1})
Of course, this does not work in MongoDB.
Considering your other question , this is one of the reasons for using an "embedded object" instead of an "array of objects". Using the "inline object" you can do the following:
coll.find({_id:'2'}, {'objects.3': 1})
This allows you to select only the "built-in objects" that you need.
This way, I do not need to select the entire parent ...
The thing with MongoDB is that the parent document is always checked out. Queries return top-level documents. It is baked throughout the architecture. Even if you request only a fragment of a document, the server should still load the entire document into memory before serving you the requested fragment.
The only way around this may be a new Aggregation Structure , but it is not yet in a stable branch.
Gates vp
source share