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

后端 未结 2 1314
时光取名叫无心
时光取名叫无心 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:38

    According to above mentioned description please try executing following query in MongoDB shell.

    db.collection.aggregate(
    
        // Pipeline
        [
            // Stage 1
            {
                $unwind: "$books"
            },
    
            // Stage 2
            {
                $group: {
                  _id:null,
                  books:{$addToSet:'$books'}
                }
            },
    
            // Stage 3
            {
                $project: {
                    books:1,
                    _id:0
                }
            },
    
        ]
    
    );
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题