0

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) {
    // ...
});
Community
  • 1
  • 1
Industrial
  • 41,400
  • 69
  • 194
  • 289
  • if I understood your question, you want to query only the subdocument without passing for the parent collection `books`? –  Feb 23 '12 at 13:42
  • Yep. Only the subdocument which is one of the `authors` in this case – Industrial Feb 23 '12 at 13:46
  • possible duplicate of [Mongodb select field to return embedded document in array](http://stackoverflow.com/questions/9201743/mongodb-select-field-to-return-embedded-document-in-array) – Andrew Orsich Feb 23 '12 at 13:53

1 Answers1

1

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.

Community
  • 1
  • 1