Mongoose aggregate Lookup - How to filter by specific id

后端 未结 2 1588
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 04:37

I am trying to make a aggregate pipeline - $lookup to receive from another collection only items that are not equal to specific _id

for example :

2条回答
  •  情歌与酒
    2021-01-06 05:29

    You can try below aggregation for mongodb version below 3.6

    db.business.aggregate([
        {$match : {_id : 1}},
        {$lookup : {from : "clinics", localField : "clinics", foreignField : "_id", as : "clinics"}},
        {$addFields : {clinics : {$filter : {input : "$clinics", as : "c", cond : {$ne : ["$$c._id", 1]}}}}}
    ]).pretty()
    

    result

    { "_id" : 1, "name" : "some business name", "clinics" : [ { "_id" : 2, "name" : "some name2" }, { "_id" : 3, "name" : "some name3" } ] }
    

提交回复
热议问题