放松后MongoDB组文件

发布于 2025-01-23 16:20:19 字数 1178 浏览 0 评论 0原文

在我的mongodb books集合中,我的文档看起来像:

    {
      _id: ObjectId(625efa44f1ba751c8275ea51),
      contributors:[ObjectId(625efa44f1ba751c8275ea52), ObjectId(625efa44f1ba751c8275ea53)]
      //other fields
    }

我想做一个查询,以返回我的文档,例如:

    {
      _id: ObjectId(625efa44f1ba751c8275ea51),
      contributors:[
                     {
                       _id: ObjectId(625efa44f1ba751c8275ea52),
                       first_name: 'Luigi'
                       //many other fields
                     },
                     {
                       _id: ObjectId(625efa44f1ba751c8275ea53),
                       first_name: 'Mario'
                       //many other fields
                     },
                   ]
      //other fields
    }

我在贡献者上做了undind 查找使用我的用户集合,现在我需要对它们进行分组。我以前从未使用过它,但是我做了类似的事情:

    {
        $group:{
                 _id: '_id'
               }
    }

但是我不知道下一步该怎么做以保留书籍和用户的所有字段。

你有什么想法吗?

In my mongodb books collection I have documents that look like:

    {
      _id: ObjectId(625efa44f1ba751c8275ea51),
      contributors:[ObjectId(625efa44f1ba751c8275ea52), ObjectId(625efa44f1ba751c8275ea53)]
      //other fields
    }

And I want to do a query that returns me documents like:

    {
      _id: ObjectId(625efa44f1ba751c8275ea51),
      contributors:[
                     {
                       _id: ObjectId(625efa44f1ba751c8275ea52),
                       first_name: 'Luigi'
                       //many other fields
                     },
                     {
                       _id: ObjectId(625efa44f1ba751c8275ea53),
                       first_name: 'Mario'
                       //many other fields
                     },
                   ]
      //other fields
    }

I did an unwind on contributors and a lookup with my users collection and now I need to group them. I haven't used it before but I did something like:

    {
        $group:{
                 _id: '_id'
               }
    }

But I don't know what to do next in order to preserve all the fields from books and also from users.

Do you have any idea?

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

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

发布评论

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

评论(1

不知所踪 2025-01-30 16:20:19

如果$查找结果足够小(< 16mb 文档尺寸限制),
您可以简单地做$查找

db.books.aggregate([
  {
    "$lookup": {
      "from": "users",
      "localField": "contributors",
      "foreignField": "_id",
      "as": "contributors"
    }
  }
])

这是 mongo playground 供您参考。

如果$查找结果将超过16 MB的限制,则您仍然可以$ utind。只需使用 $ first $ untind之后重组其他字段。

db.books.aggregate([
  {
    "$lookup": {
      "from": "users",
      "localField": "contributors",
      "foreignField": "_id",
      "as": "contributors"
    }
  },
  {
    "$unwind": "$contributors"
  },
  {
    "$group": {
      "_id": "_id",
      bookName: {
        $first: "$bookName"
      },
      "contributors": {
        "$push": "$contributors"
      }
    }
  }
])

这是蒙哥游乐场供您参考。

If the $lookup result is small enough (<16MB document size limit),
you can simply do a $lookup.

db.books.aggregate([
  {
    "$lookup": {
      "from": "users",
      "localField": "contributors",
      "foreignField": "_id",
      "as": "contributors"
    }
  }
])

Here is the Mongo Playground for your reference.

If the $lookup result will exceed 16 MB limit, you can still $unwind. Just use$firstto regroup the other fields after the$unwind`

db.books.aggregate([
  {
    "$lookup": {
      "from": "users",
      "localField": "contributors",
      "foreignField": "_id",
      "as": "contributors"
    }
  },
  {
    "$unwind": "$contributors"
  },
  {
    "$group": {
      "_id": "_id",
      bookName: {
        $first: "$bookName"
      },
      "contributors": {
        "$push": "$contributors"
      }
    }
  }
])

Here is the Mongo playground for your reference.

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