$replaceRoot in mongodb aggregation

前端 未结 2 1835
孤城傲影
孤城傲影 2021-01-21 17:15

I have a collection like this:

{
    \"_id\" : ObjectId(\"5bd1686ba64b9206349284db\"),
    \"type\" : \"Package\",
    \"codeInstances\" : [ 
        {
                  


        
2条回答
  •  轮回少年
    2021-01-21 17:25

    You can try below aggregation using $replaceRoot

    db.collection.aggregate([
      { "$match": { "codeInstances.name": "b", "type": "Package" }},
      { "$unwind": "$codeInstances" },
      { "$match": { "codeInstances.name": "b", "type": "Package" }},
      { "$replaceRoot": { "newRoot": "$codeInstances" }}
    ])
    

提交回复
热议问题