MongoDB $查找在另一个表格中,带有匹配字段作为数组

发布于 2025-01-24 16:51:31 字数 1579 浏览 0 评论 0 原文

我试图返回其他集合中的数组中存在真正的基于错误的值。

假设我的活动收集如下。

{"_id":{"$oid":"626aa83ca3d690ffe4a40103"},"name":"event1","title":"Event 1"}
{"_id":{"$oid":"626aa83ca3d690ffe4a40104"},"name":"event2","title":"Event 2"}
{"_id":{"$oid":"626aa83ca3d690ffe4a40105"},"name":"event3","title":"Event 3"}

我的用户集合如下:

{"_id":{"$oid":"62149d30950b000a31448eb3"},"userName":"[email protected]","commentDictionary":"{}","__v":0,"createdAt":{"$date":"2022-02-22T08:22:08.161Z"},"updatedAt":{"$date":"2022-04-27T07:11:58.999Z"},"passwordResetLinkExpiryDate":{"$date":"2022-03-22T16:40:15.356Z"},"_eventIds":[{"$oid":"626aa83ca3d690ffe4a40103"},{"$oid":"626aa83ca3d690ffe4a40105"}]}

取决于 _Eventids 在用户集合中的匹配值,我正在寻找如下的输出。这也是在用户名中具有

{"_id":{"$oid":"626aa83ca3d690ffe4a40103"},"name":"event1","title":"Event 1", attended: true}
{"_id":{"$oid":"626aa83ca3d690ffe4a40104"},"name":"event2","title":"Event 2", attended: false}
{"_id":{"$oid":"626aa83ca3d690ffe4a40105"},"name":"event3","title":"Event 3", attended: true}

我正在尝试使用$查找,但是它需要一个匹配的密钥,但就我而言,它是数组。因此无法为此删除逻辑。

I am trying to return true false based value exists in array in other collection.

Let's say my Event collection is as following.

{"_id":{"$oid":"626aa83ca3d690ffe4a40103"},"name":"event1","title":"Event 1"}
{"_id":{"$oid":"626aa83ca3d690ffe4a40104"},"name":"event2","title":"Event 2"}
{"_id":{"$oid":"626aa83ca3d690ffe4a40105"},"name":"event3","title":"Event 3"}

My User Collection is as following:

{"_id":{"$oid":"62149d30950b000a31448eb3"},"userName":"[email protected]","commentDictionary":"{}","__v":0,"createdAt":{"$date":"2022-02-22T08:22:08.161Z"},"updatedAt":{"$date":"2022-04-27T07:11:58.999Z"},"passwordResetLinkExpiryDate":{"$date":"2022-03-22T16:40:15.356Z"},"_eventIds":[{"$oid":"626aa83ca3d690ffe4a40103"},{"$oid":"626aa83ca3d690ffe4a40105"}]}

depending on matching values in _eventIds in User Collection, I am looking for output as following. And that is also in userName having value as [email protected]

{"_id":{"$oid":"626aa83ca3d690ffe4a40103"},"name":"event1","title":"Event 1", attended: true}
{"_id":{"$oid":"626aa83ca3d690ffe4a40104"},"name":"event2","title":"Event 2", attended: false}
{"_id":{"$oid":"626aa83ca3d690ffe4a40105"},"name":"event3","title":"Event 3", attended: true}

I was trying with $lookup but it needs one matching key, but in my case it is Array. So unable to figure logic for this.

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

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

发布评论

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

评论(1

誰認得朕 2025-01-31 16:51:31

查询

  • 查找在单个值/数组上工作,并且如果数组包含此值,
  • 而不是进行所有将慢的查找,更多的内存等, $ limit 1 is使用的,因为我们只关心是否找到匹配
  • ,结果是空的=> ACTENTED = false;否则参加= true
Event.aggregate(
[{"$lookup": 
   {"from": "User",
    "localField": "_id",
    "foreignField": "_eventIds",
    "pipeline": [{"$limit": 1}],
    "as": "attended"}},
 {"$set": 
   {"attended": {"$cond": [{"$eq": ["$attended", []]}, false, true]}}}])

Query

  • lookup works on single value/array, and matches if the array contains this value
  • instead of doing all the lookup that will be slower, more memory etc, $limit 1 is used, because we only care if match was found or not
  • if results are empty => attented = false else attended=true

Playmongo

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