How would I select the subdocument author, in the below shown object, without directly accessing it, ie authors[0], so that the result that I get back is {"_id":"4f44af6a024342300e000002", visible: true}?
The object:
{
_id: "4f44af6a024342300e000001",
title: "A book",
created: "2012-02-22T14:12:51.305Z"
authors: [{"_id":"4f44af6a024342300e000002", visible: true}]
}
I have been able to update it by doing this, but selecting it seems like a completely different story:
books.update({_id: "4f44af6a024342300e000001",'authors._id': "4f44af6a024342300e000002"},
{$set: {'books.$.visible': true}}, function(err) {
// ...
});
It is currently not possible (see a similar question with extra details)
In MongoDB, how does on get the value in a field for an embedded document, but query based on a different value
In the next version, the aggreation framework could give some step in this direction, but the real solution are the virtual collection.
For the time being (in short), use an embedded document only if you go through the parent.