【问题标题】:MongoDB- find document by id then group it with keyMongoDB- 按 id 查找文档,然后用键对其进行分组
【发布时间】:2021-11-05 12:36:14
【问题描述】:

我的数据结构如下所示:

[
  {
    "_id": "1",
    "title": "Yamaha",
    "data": "Sed ut perspiciatis",
    "type": "Bike"
  },
  {
    "_id": "2",
    "title": "Pulsar",
    "data": "Quis autem vel eum",
    "type": "Bike"
  },
  {
    "_id": "3",
    "title": "Tesla Model Y",
    "data": "because it is pleasure",
    "type": "Car"
  },
  {
    "_id": "4",
    "title": "Harley-Davidson",
    "data": "praising pain was born",
    "type": "Bike"
  },
  {
    "_id": "6",
    "title": "Mustang",
    "data": "non numquam eius",
    "type": "Car"
  },
  {
    "_id": "7",
    "title": "BMW",
    "data": "Man of Culture",
    "type": "Car"
  }
]

现在,从前端用户可以使用他们的唯一_id从数据库中搜索任何项目,如下所示:

db.collection.find({_id: "3" })

返回以下内容:

[
  {
    "_id": "3",
    "data": "because it is pleasure",
    "title": "Tesla Model Y",
    "type": "Car"
  }
]

问题部分:

现在,包括上面返回的文档,我还想返回那些匹配 type 值的文档


我的问题意味着;如果用户正在查找任何带有其特定 _id 的文档。假设 3 那么它应该返回以下内容:

找到具有唯一_id$grouptype字段值的项目

  [{
    "_id": "3",
    "title": "Tesla Model Y",
    "data": "because it is pleasure",
    "type": "Car"
  }
  {
    "_id": "6",
    "title": "Mustang",
    "data": "non numquam eius",
    "type": "Car"
  },
  {
    "_id": "7",
    "title": "BMW",
    "data": "Man of Culture",
    "type": "Car"
  }]

这可能吗?是否可以 $group finding By Id 之后的文档?我尝试了几种方法来做到这一点,但每种方法都没有用。对于这个复杂的要求

,任何建议都会有所帮助

:)

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    查询

    • 自行查找,仅加入 id=3 所具有的类型。
    • 空连接结果 => 不同的类型,因此它们被过滤掉了

    Test code here

    db.collection.aggregate([
      {
        "$lookup": {
          "from": "collection",
          "let": {
            "type": "$type"
          },
          "pipeline": [
            {
              "$match": {
                "$expr": {
                  "$and": [
                    {
                      "$eq": [
                        "$_id",
                        "3"
                      ]
                    },
                    {
                      "$eq": [
                        "$$type",
                        "$type"
                      ]
                    }
                  ]
                }
              }
            }
          ],
          "as": "joined"
        }
      },
      {
        "$match": {
          "$expr": {
            "$ne": [
              "$joined",
              []
            ]
          }
        }
      },
      {
        "$unset": [
          "joined"
        ]
      }
    ])
    

    【讨论】:

    • 废话,我从没想过有人会花时间这样做! :D 尽管我会说这是一个非常糟糕的查询,而且速度非常慢(因为它为每个条目创建一个对象,然后通过比较对其进行过滤),但花时间编写它的荣誉:)
    • 它的集合扫描,但是查找可以使用索引。我不认为我们可以通过 1 个查询和没有集合扫描来做到这一点,因为我们不知道我们在寻找什么。
    • @Takis_ 感谢您的回答。但是第二个 $match 阶段 ($lookup ) 之后没有在 MongoDB Atlas Aggegration Pipeline Builder 中显示任何结果。但它在操场上工作。为什么?
    • @Takis_ 我也见过这个解决方案:Link,但是第二个 $match 不起作用。你有什么想法吗?
    • 我不知道,我在我的司机和操场上测试过
    【解决方案2】:

    您基本上是在混合两个单独的查询:

    1. 按 ID 获取项目 - 返回单个项目
    2. 获取与第一个项目类型相同的项目列表 - 返回一个项目列表

    由于查询的不同,没有超级直接的方法可以做到这一点。当然,您可以使用$aggregate 来解决问题,但从逻辑上讲,您仍然需要从数据库中查询很多内容,并且您必须更深入地挖掘才能正确优化它。

    只要不是查询几千万条记录,为了简单起见,我建议你一个接一个地做这两个查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2019-06-06
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多