How to return just the nested documents of an array from all documents

You can do this using .aggregate() and predominantly the $unwind pipeline operator:

In modern MongoDB 3.4 and above you can use in tandem with $replaceRoot

Model.aggregate([
  { "$unwind": "$books" },
  { "$replaceRoot": { "newRoot": "$books" } }
],function(err,results) {

})

In earlier versions you specify all fields with $project:

Model.aggregate([
  { "$unwind": "$books" },
  { "$project": {
    "_id": "$books._id",
    "pages": "$books.pages",
    "title": "$books.title"
  }}
],function(err,results) {

})

So $unwind is what you use to deconstruct or “denormalise” the array entries for processing. Effectively this creates a copy of the whole document for each member of the array.

The rest of the task is about returning “only” those fields present in the array.

It’s not a very wise thing to do though. If your intent is to only return content embedded within an array of a document, then you would be better off putting that content into a separate collection instead.

It’s far better for performance, pulling apart a all documents from a collection with the aggregation framework, just to list those documents from the array only.

Leave a Comment