首页 > 解决方案 > 如何使用聚合对 mongodb 进行分组

问题描述

我正在尝试用他的才能和人才媒体填充我的用户文档。但我得到了重复的对象。我想将人才作为一个对象,并让媒体在一个阵列中反对该人才。

我的用户模型:

{
    _id: '5f1acd6e6985114f2c1567ea',
    name: 'test user',
    email: 'test@email.com'
}
    

人才模型

{
    _id: '5f1acd6e6985114f2c1567fa',
    categoryId: '5f1acd6e6985114f2c1567ga',
    userId: '5f1acd6e6985114f2c1567ea'
    level: '5',
}

人才媒体模式

{
    _id: 5f1acd6e6985114f2c156710',
    talentId: '5f1acd6e6985114f2c1567fa',
    media: 'file.jpg',
    fileType: 'image'
}

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

{
    _id: '5f1acd6e6985114f2c1567ga',
    title: 'java'
}

我想要的结果如下

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 文档中找到的大部分不推荐使用。有这样的人才模型是不是更好,

{
    _id: '5f1acd6e6985114f2c1567fa',
    categoryId: '5f1acd6e6985114f2c1567ga',
    userId: '5f1acd6e6985114f2c1567ea'
    level: '5',
    medias: [
        {
           _id: '',
           file: 'file1.jpg',
           fileType: 'image'
        },
        {
           _id: '',
           file: 'file2.jpg',
           fileType: 'image'
        },
    ]
}

标签: node.jsmongodbmongoose

解决方案


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

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
    }
  }
])

*您可能必须调整集合名称。

Mongo游乐场


推荐阅读