How to use $elemMatch on aggregate's projection?

前端 未结 7 2015
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 15:27

This is my object:

{ \"_id\" : ObjectId(\"53fdcb6796cb9b9aa86f05b9\"), \"list\" : [ \"a\", \"b\" ], \"complist\" : [ { \"a\" : \"a\", \"b\" : \"b\" }, { \"a\         


        
7条回答
  •  情深已故
    2021-01-01 16:07

    Although the question is old, here is my contribution for November 2017.

    I had similar problem and doing two consecutive match operations worked for me. The code below is a subset of my whole code and I changed elements names, so it's not tested. Anyway this should point you in the right direction.

    db.collection.aggregate([
        {
            "$match": {
                "_id": "ID1"
            }
        },
        {
            "$unwind": "$sub_collection"
        },
        {
            "$match": {
                "sub_collection.field_I_want_to_match": "value"
            }
        }
    ])
    

提交回复
热议问题