首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用聚合对mongodb进行分组

如何使用聚合对mongodb进行分组
EN

Stack Overflow用户
提问于 2020-07-27 13:54:01
回答 1查看 50关注 0票数 1

我正试图用他的才能和人才媒体来填充我的用户文档。但我得到了重复的东西。我想要获得天赋作为一个对象和媒体与该天赋在数组中。

我的用户模型:

代码语言:javascript
复制
{
    _id: '5f1acd6e6985114f2c1567ea',
    name: 'test user',
    email: 'test@email.com'
}

人才模型

代码语言:javascript
复制
{
    _id: '5f1acd6e6985114f2c1567fa',
    categoryId: '5f1acd6e6985114f2c1567ga',
    userId: '5f1acd6e6985114f2c1567ea'
    level: '5',
}

人才传媒模式

代码语言:javascript
复制
{
    _id: 5f1acd6e6985114f2c156710',
    talentId: '5f1acd6e6985114f2c1567fa',
    media: 'file.jpg',
    fileType: 'image'
}

我有另一个存储类别的模型

代码语言:javascript
复制
{
    _id: '5f1acd6e6985114f2c1567ga',
    title: 'java'
}

我要的结果如下

代码语言:javascript
复制
user: {
  _id: '',
  name: 'test user',
  email: 'test@email.com',
  talents: [
    {
      _id: '',
      level: '5',
      categoryId: {
        _id: '',
        title: 'java'
      },
      medias: [
        {
           _id: '',
           file: 'file1.jpg',
           fileType: 'image'
        },
        {
           _id: '',
           file: 'file2.jpg',
           fileType: 'image'
        },
      ]
    }
  ]
}

我还试着在人才文档中添加人才媒体。但在MongoDB中,多数情况下不推荐查找文档。有这样的天才模特更好吗,

代码语言:javascript
复制
{
    _id: '5f1acd6e6985114f2c1567fa',
    categoryId: '5f1acd6e6985114f2c1567ga',
    userId: '5f1acd6e6985114f2c1567ea'
    level: '5',
    medias: [
        {
           _id: '',
           file: 'file1.jpg',
           fileType: 'image'
        },
        {
           _id: '',
           file: 'file2.jpg',
           fileType: 'image'
        },
    ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-27 14:08:17

您可以通过多次使用$lookup来实现这一点:

代码语言:javascript
复制
db.users.aggregate([
  {
    $lookup: {
      from: "talents",
      let: {
        userId: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$$userId",
                "$userId"
              ]
            }
          }
        },
        {
          $lookup: {
            from: "categories",
            let: {
              categoryId: "$categoryId"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $eq: [
                      "$$categoryId",
                      "$_id"
                    ]
                  }
                }
              },
              {
                $project: {
                  _id: "",
                  title: 1
                }
              }
            ],
            as: "categoryId"
          }
        },
        {
          $lookup: {
            from: "talent_media",
            let: {
              talentId: "$_id"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $eq: [
                      "$$talentId",
                      "$talentId"
                    ]
                  }
                }
              },
              {
                $project: {
                  _id: "",
                  file: "$media",
                  fileType: "$fileType"
                }
              }
            ],
            as: "medias"
          }
        },
        {
          $unwind: {
            path: "$categoryId",
            preserveNullAndEmptyArrays: true
          }
        },
        {
          $project: {
            _id: "",
            categoryId: 1,
            level: 1,
            medias: 1
          }
        }
      ],
      as: "talents"
    }
  },
  {
    $project: {
      _id: "",
      email: 1,
      name: 1,
      talents: 1
    }
  }
])

*您可能需要调整集合名称。

MongoPlayground

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63116858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档