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

后端 未结 2 1338
时光取名叫无心
时光取名叫无心 2020-11-27 22:59

I have a question about querying nested documents. I tried to search but nothing answered my question or I am maybe overlooking it. I have structure like this:



        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 23:42

    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.

提交回复
热议问题