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

后端 未结 2 1330
时光取名叫无心
时光取名叫无心 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
                }
            },
    
        ]
    
    );
    

提交回复
热议问题