MongoDB聚集多个查找条件

发布于 2025-01-24 03:24:44 字数 1114 浏览 0 评论 0原文

我有3个收藏。

db.a.insert([
  { "_id" : ObjectId("5b56989172ebcb11105e8f41"), "db_type":b, "number" : 1},
  { "_id" : ObjectId("5b56989172ebcb11105e8f42"), "db_type":c, "number" : 2},
])

db.b.insert([
  { "_id" : ObjectId("5b56989172ebcb11105e8f43"), "number" : 1, "value" : "111"},
])

db.c.insert([
  { "_id" : ObjectId("5b56989172ebcb11105e8f44"), "number" : 2, "value" : "222"},
])

我想根据db_type进行查找查询,以从每个集合中获取值。

在这种情况下,我该怎么办?

结果:

{ "_id" : ObjectId("5b56989172ebcb11105e8f41"), "db_type" : b, "number" : 1, "value" : "111"}
{ "_id" : ObjectId("5b56989172ebcb11105e8f42"), "db_type" : c, "number" : 2, "value" : "222"}

堵塞零件...

db.getCollection('a').aggregate([
    {
        "$lookup":{
            "from":         "b" or "c", // I want to give condition here.
            "localField":   "number",
            "foreignField": "number",
            "as":           "result"
        }
    },
])

I have 3 collection.

db.a.insert([
  { "_id" : ObjectId("5b56989172ebcb11105e8f41"), "db_type":b, "number" : 1},
  { "_id" : ObjectId("5b56989172ebcb11105e8f42"), "db_type":c, "number" : 2},
])

db.b.insert([
  { "_id" : ObjectId("5b56989172ebcb11105e8f43"), "number" : 1, "value" : "111"},
])

db.c.insert([
  { "_id" : ObjectId("5b56989172ebcb11105e8f44"), "number" : 2, "value" : "222"},
])

I want to make a lookup query that gets values from each collection according to db_type.

What should I do in this case?

result :

{ "_id" : ObjectId("5b56989172ebcb11105e8f41"), "db_type" : b, "number" : 1, "value" : "111"}
{ "_id" : ObjectId("5b56989172ebcb11105e8f42"), "db_type" : c, "number" : 2, "value" : "222"}

clogged part...

db.getCollection('a').aggregate([
    {
        "$lookup":{
            "from":         "b" or "c", // I want to give condition here.
            "localField":   "number",
            "foreignField": "number",
            "as":           "result"
        }
    },
])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2025-01-31 03:24:44

对于您的情况,由于您只有2个情况bc可以从中查找。您可以简单地进行2个单独的查找,然后使用$ setunion将结果分组在一起。

db.a.aggregate([
  {
    "$lookup": {
      "from": "b",
      "let": {
        db_type: "$db_type",
        number: "$number"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$db_type",
                    "b"
                  ]
                },
                {
                  $eq: [
                    "$number",
                    "$number"
                  ]
                }
              ]
            }
          }
        }
      ],
      "as": "bLookup"
    }
  },
  {
    "$lookup": {
      "from": "c",
      "let": {
        db_type: "$db_type",
        number: "$number"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$db_type",
                    "c"
                  ]
                },
                {
                  $eq: [
                    "$number",
                    "$number"
                  ]
                }
              ]
            }
          }
        }
      ],
      "as": "cLookup"
    }
  },
  {
    "$addFields": {
      "allLookup": {
        "$setUnion": [
          "$bLookup",
          "$cLookup"
        ]
      }
    }
  }
])

这是 mongo playground 供您参考。

For your case, as you have only 2 cases b and c to lookup from. You can simply do 2 separate lookups and use $setUnion to group the results together.

db.a.aggregate([
  {
    "$lookup": {
      "from": "b",
      "let": {
        db_type: "$db_type",
        number: "$number"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$db_type",
                    "b"
                  ]
                },
                {
                  $eq: [
                    "$number",
                    "$number"
                  ]
                }
              ]
            }
          }
        }
      ],
      "as": "bLookup"
    }
  },
  {
    "$lookup": {
      "from": "c",
      "let": {
        db_type: "$db_type",
        number: "$number"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$db_type",
                    "c"
                  ]
                },
                {
                  $eq: [
                    "$number",
                    "$number"
                  ]
                }
              ]
            }
          }
        }
      ],
      "as": "cLookup"
    }
  },
  {
    "$addFields": {
      "allLookup": {
        "$setUnion": [
          "$bLookup",
          "$cLookup"
        ]
      }
    }
  }
])

Here is the Mongo playground for your reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文