簡體   English   中英

如何在 MongoDb .aggregate() 中的 $lookup 中添加條件?

[英]How to add a condition inside $lookup in MongoDb .aggregate()?

人員集合:

{
  "_id": ObjectId("5f3258cfbaaccedaa5dd2c96"),
  "gender": "male",
  "name": {
    "title": "mr",
    "first": "victor",
    "last": "pedersen",//... more properties
} 

個人資料收集:

{
      "_id": ObjectId("5f3a91e68b1c26e68f9ed3ad"),
      "country": "India",
      "personid": ObjectId("5f3258cfbaaccedaa5dd2c96")
}

從個人以及相關個人詳細信息中獲取文件,其中個人詳細信息中的關聯國家是“印度

因此,如果 10 人中只有 3 人來自印度。 我應該在結果集中獲得 3 個人文檔及其相關的個人詳細信息

詢問:

 [
    {
      "$match": {
      "$or": [
         
        {
          "$expr": {
            "$eq": [
              "$gender",
              "male"
            ]
          }
        }
      ]
    }
  },
  {
    "$facet": {
      "totalCount": [
        {
          "$count": "value"
        }
      ],
      "data": [
        {
          "$project": {
            "_id": "$_id",
            "fname": "$name.first",
            "lname": "$name.last",
            "dobage": "$dob.age",
            "registeredAge": "$registered.age"                
          }
        },
        {
          "$sort": {
            "name.first": 1
          }
        } ,
        {
          "$lookup": {
            "from": "persondetails",
            "localField": "_id",
            "foreignField": "personid", // how to add where clause in this lookup?
            "as": "persondetail" 
          }
        }
      ]
    }
  }
]

編輯:

https://mongoplayground.net/p/3vBs6Frt-aK

預期結果:

[
  {
    "data": [
      {
        "_id": ObjectId("5f3258cfbaaccedaa5dd2c96"),
        "fname": "victor",
        "lname": "pedersen",
        "persondetail": [
          {
            "_id": ObjectId("5f3a91e68b1c26e68f9ed3ad"),
            "country": "India",
            "personid": ObjectId("5f3258cfbaaccedaa5dd2c96")
          }
        ]
      }],
    "totalCount": [
      {
        "value": 1
      }
    ]
  }
]

還有第二個$lookup語法允許您指定自定義過濾條件:

{
    "$lookup": {
        "from": "persondetails",
        "let": { person_id: "$_id" },
        "pipeline": [
            {
                $match: {
                    $expr: {
                        $and: [
                            { $eq: [ "$$person_id", "$personid" ] },
                            { $eq: [ "$country", "India" ] },
                        ]
                    }
                }
            }
        ],
        "as": "persondetail" 
    }
}

編輯:您還需要添加 $match 來過濾掉persondetail為空的人,如果您希望將其包含在您的計數中,那么您需要運行 $facet 作為最后一個操作:

[
    {
        "$match": {
            "$or": [ {"$expr": { "$eq": [ "$gender", "male" ] } } ]
        }
    },
    {
        "$project": {
                "_id": "$_id",
                "fname": "$name.first",
                "lname": "$name.last",
                "dobage": "$dob.age",
                "registeredAge": "$registered.age"                
        }
    },    
    {
        "$lookup": {
            "from": "persondetails",
            "let": { person_id: "$_id" },
            "pipeline": [
                {
                    $match: {
                        $expr: {
                            $and: [
                                { $eq: [ "$$person_id", "$personid" ] },
                                { $eq: [ "$country", "India" ] },
                            ]
                        }
                    }
                }
            ],
            "as": "persondetail" 
        }
    },
    {
        $match: {
            persondetail: { $ne: [] }
        }
    },
    {
        $facet: {
            totalCount: [ { $count: "value" } ],
            data: [ { $sort: { "name.first": 1 } },  ]
        }
    }
]

蒙戈游樂場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM