mongodb在_id字段上从字符串到objectid grompate $查找

发布于 2025-01-25 07:54:17 字数 1151 浏览 2 评论 0 原文

我有两个收藏。

客户:

{
   "_id" : ObjectId("584aac38686860d502929b8b"),
   "name" : "user",
   "email" : "[email protected]"
}

帖子:

{
   "_id" : ObjectId("584aaca6686860d502929b8d"),
   "title" : "Post",
   "description":"description",
   "user_id" : "584aac38686860d502929b8b"  
}

我想基于User_id(来自 ports Collection) - _id(在>客户 Collection)。

我尝试了以下查询:

dbo.collection('customers').aggregate([
        {
            $lookup:
                {
                    from: 'posts',
                    localField: 'user_id',
                    foreignField: '_id',
                    as: 'posts'
                }
        }
    ])

但是它不起作用。

我得到的输出:

{
    "_id": "584aac38686860d502929b8b",
    "name": "user",
    "email": "[email protected]",
    "posts": []
}

I have two collections.

Customers:

{
   "_id" : ObjectId("584aac38686860d502929b8b"),
   "name" : "user",
   "email" : "[email protected]"
}

Posts:

{
   "_id" : ObjectId("584aaca6686860d502929b8d"),
   "title" : "Post",
   "description":"description",
   "user_id" : "584aac38686860d502929b8b"  
}

I want to join this collection based on the user_id (from posts collection) - _id ( in customers collection).

I tried the below query:

dbo.collection('customers').aggregate([
        {
            $lookup:
                {
                    from: 'posts',
                    localField: 'user_id',
                    foreignField: '_id',
                    as: 'posts'
                }
        }
    ])

but it's not working.

The output I am getting:

{
    "_id": "584aac38686860d502929b8b",
    "name": "user",
    "email": "[email protected]",
    "posts": []
}

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

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

发布评论

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

评论(1

北渚 2025-02-01 07:54:17

从附件帖子收集, user_id 是字符串,但不是 objectid

要进行比较,您必须先将 user_id 将其转换为 objectId 首先。

db.customers.aggregate([
  {
    $lookup: {
      from: "posts",
      let: {
        customerId: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                {
                  "$toObjectId": "$user_id"
                },
                "$customerId"
              ]
            }
          }
        }
      ],
      as: "posts"
    }
  }
])

示例mongo playground


注意:从您现有的 $ lookup 中,您已经有反向<代码> localfield 和 foreferfield

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}

From attached posts collection, user_id was a string but not ObjectId.

To compare, you have to convert user_id to ObjectId first.

db.customers.aggregate([
  {
    $lookup: {
      from: "posts",
      let: {
        customerId: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                {
                  "$toObjectId": "$user_id"
                },
                "$customerId"
              ]
            }
          }
        }
      ],
      as: "posts"
    }
  }
])

Sample Mongo Playground


Note: From your existing $lookup, you have reversed localField and foreignField.

Equality Match with a Single Join Condition

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文