Mongoose aggregate Lookup - How to filter by specific id

后端 未结 2 1581
没有蜡笔的小新
没有蜡笔的小新 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:17

    You can use below aggregation with mongodb 3.6 and above

    You need to just use $match with the child collection as you do with the parent collection in the first stage.

    db.BusinessCollection.aggregate([
      { "$match": { "clinics": { "$type": "array" }}},
      { "$lookup": {
        "from": "ClinicsCollection",
        "let": { "clinics": "$clinics" },
        "pipeline": [
          { "$match": {
            "$expr": {
              "$and": [
                { "$in": ["$_id", "$$clinics"] },
                { "$not": { "$eq": ["$_id", 1] }}
              ]
            }
          }}
        ],
        "as": "clinics"
      }}
    ])
    

提交回复
热议问题