从集合中获取所有记录,如果匹配 MongoDB 上第二个集合中的至少一条记录

问题描述 投票:0回答:1

我有两个收藏。用户和课程

用户收藏

  [{
    "_id": "11111",
    "name": "john",
  },
  {
    "_id": "11112",
    "name": "smith",
    
  }]

课程合集

[{
    "_id": "00011",
    "user_id": "11111",
    "location_id": "9999",
  },
  {
    "_id": "00012",
    "user_id": "11111",
    "location_id": "8888",
    
  },
  {
    "_id": "00013",
    "user_id": "11111",
    "location_id": "7777",
  },
  {
    "_id": "00014",
    "user_id": "11112",
    "location_id": "7777",
  }]

如果我按region_id 7777应用过滤器,那么我想要以下输出。如果我应用区域 ID 7777 和 8888,我想要相同的输出。所以基本上,我想要所有用户区域,如果它至少匹配一个region_id。如果没有 Region_id 过滤器,我会得到正确的响应 预期结果:

[
    {
      "_id": "11111",
      "name": "john",
      "regions": [
        {
          "_id": "00011",
          "user_id": "11111",
          "location_id": "9999"
        },
        {
          "_id": "00012",
          "user_id": "11111",
          "location_id": "8888"
        },
        {
          "_id": "00013",
          "user_id": "11111",
          "location_id": "7777"
        }
      ]
    },
    {
      "_id": "11112",
      "name": "smith",
      "regions": [
        {
          "_id": "00014",
          "user_id": "11112",
          "location_id": "7777"
        }
      ]
    }
]

以下是我的聚合查询

db.user.aggregate([
  {
    "$match": {}
  },
  {
    "$lookup": {
      "from": "region",
      "localField": "_id",
      "foreignField": "user_id",
      "as": "regions"
    }
  },
  {
    "$addFields": {
      "regions": {
        "$filter": {
          input: "$regions",
          as: "region",
          cond: {
            $in: [
              "$$region.location_id",
              [
                "7777"
              ]
            ]
          }
        }
      }
    }
  }
])

实际结果(如果我应用过滤器region_id:7777,我得到的结果低于结果)

[
    {
      "_id": "11111",
      "name": "john",
      "regions": [
        {
          "_id": "00013",
          "user_id": "11111",
          "location_id": "7777"
        }
      ]
    },
    {
      "_id": "11112",
      "name": "smith",
      "regions": [
        {
          "_id": "00014",
          "user_id": "11112",
          "location_id": "7777"
        }
      ]
    }
]
mongodb mongodb-query aggregation-framework
1个回答
0
投票

不完全了解您的过滤要求,尤其是在输入过滤列表中没有匹配的情况下(例如[“6666”],因此没有匹配到任何区域)。但我猜您想对

$filter
执行以下操作:在
$anyElementTrue
中的
$lookup
之后应用
$filter

db.user.aggregate([
  {
    "$lookup": {
      "from": "region",
      "localField": "_id",
      "foreignField": "user_id",
      "as": "regions"
    }
  },
  {
    "$set": {
      "regions": {
        "$filter": {
          "input": "$regions",
          "as": "r",
          "cond": {
            "$anyElementTrue": {
              "$map": {
                "input": "$regions",
                "as": "rr",
                "in": {
                  "$in": [
                    "$$rr.location_id",
                    [
                      // your input filter here
                      "7777",
                      "8888"
                    ]
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
])

蒙戈游乐场

© www.soinside.com 2019 - 2024. All rights reserved.