MongoDB使用一个集合中的一系列ID来根据另一个集合的ID进行过滤结果

发布于 2025-02-13 12:16:22 字数 700 浏览 0 评论 0原文

假设有一个MongoDB集合Colla。说colla具有以下三个文档:

{"_id": "1", "name": "Bob"},
{"_id": "2", "name": "John"},
{"_id": "3", "name": "Will"}

假设还有另一个MongoDB Collection CollB具有以下文档:

{
    "_id": "1", 
    "foo": {
        "arr": ["1", "3"]
    }
}

MongoDB中是否有办法查询collb foo.arr数组内容([“ 1”],[“ 3”]),并使用它来检索文档Colla具有这些_id值,以产生以下结果:

{"_id": "1", "name": "Bob"},
{"_id": "3", "name": "Will"}

Suppose there is a MongoDB Collection CollA. Say CollA has the following three Documents:

{"_id": "1", "name": "Bob"},
{"_id": "2", "name": "John"},
{"_id": "3", "name": "Will"}

Let's say there is another MongoDB Collection CollB which has the following Document:

{
    "_id": "1", 
    "foo": {
        "arr": ["1", "3"]
    }
}

Is there a way in MongoDB to query CollB for the foo.arr array contents (["1"],["3"]) and use it to retrieve the Documents in CollA which have these _id values, to produce the following result:

{"_id": "1", "name": "Bob"},
{"_id": "3", "name": "Will"}

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

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

发布评论

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

评论(2

恋你朝朝暮暮 2025-02-20 12:16:24

collb执行简单$查找。在$ lookup结果 $ undind 之后,使用$替换 $ lookup

db.CollB.aggregate([
  {
    "$lookup": {
      "from": "CollA",
      "localField": "foo.arr",
      "foreignField": "_id",
      "as": "collALookup"
    }
  },
  {
    "$unwind": "$collALookup"
  },
  {
    "$replaceRoot": {
      "newRoot": "$collALookup"
    }
  }
])

结果是 mongo playground 供您参考。

Perform a simple $lookup from CollB. Use $replaceRoot with the $lookup result after $unwind

db.CollB.aggregate([
  {
    "$lookup": {
      "from": "CollA",
      "localField": "foo.arr",
      "foreignField": "_id",
      "as": "collALookup"
    }
  },
  {
    "$unwind": "$collALookup"
  },
  {
    "$replaceRoot": {
      "newRoot": "$collALookup"
    }
  }
])

Here is the Mongo Playground for your reference.

踏雪无痕 2025-02-20 12:16:24

查询

  • 查找_id带有数组foo.arr和限制1的字段,我们只是在意是否加入
    无需将许多文档保存

存储如果您只需要1个collb的特定文档,我认为从collb开始像雷的答案一样有意义,但是在$ match $ yougp $ lookup 之前,请添加$ match。 collb)

playmongo

collA.aggregate(
[{"$lookup": 
   {"from": "collB",
    "localField": "_id",
    "foreignField": "foo.arr",
    "pipeline": [{"$limit": 1}],
    "as": "results"}},
 {"$match": {"$expr": {"$ne": ["$results", []]}}},
 {"$unset": ["results"]}])

Query

  • lookup _id field with array foo.arr and limit 1, we just care if joined
    (no need to save many documents to memory)
  • keep documents that joined (not empty results)
  • unset the results field

*this will keep from collA all the documents that their _id, are in any array in collB (if you want only for 1 specific document of collB i think starting from collB like ray's answer makes more sense, but add a $match also before the $lookup for the 1 member of collB)

Playmongo

collA.aggregate(
[{"$lookup": 
   {"from": "collB",
    "localField": "_id",
    "foreignField": "foo.arr",
    "pipeline": [{"$limit": 1}],
    "as": "results"}},
 {"$match": {"$expr": {"$ne": ["$results", []]}}},
 {"$unset": ["results"]}])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文