提问者:小点点

如何使用聚合对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'
        },
    ]
}

共2个答案

匿名用户

您可以通过多次使用$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
    }
  }
])

*您可能需要修改集合名称。

蒙古游乐场

匿名用户

您必须使用嵌套的$LOOKUP,

  • 使用天赋收集查找并匹配用户ID
  • $match用于特定的user_id文档,此处有注释
db.User.aggregate([
  /** add here
  Optional for single user
  {
    $match: {
      _id: "XXXXXXXXXXXX"
    }
  },
  */
  {
    $lookup: {
      from: "Talent",
      as: "talents",
      let: {
        userId: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$$userId",
                "$userId"
              ]
            }
          }
        },
  • 我们现在在人才文档中,使用类别集合进行查找,并与categoryid
  • 匹配
  • $展开类别,因为它将返回数组,而我们需要对象
        {
          $lookup: {
            from: "Category",
            as: "categoryId",
            localField: "categoryId",
            foreignField: "_id"
          }
        },
        {
          $unwind: {
            path: "$categoryId",
            preserveNullAndEmptyArrays: true
          }
        },
  • 我们再次在人才文档中使用Talent Media Collection查找,并使用TalentId
  • 匹配
  • $project对于添加/删除不需要的字段,请在此处从TalentMedia中删除TalentIdnad从Talent
  • 中删除UserId
        {
          $lookup: {
            from: "TalentMedia",
            as: "medias",
            let: {
              talentId: "$_id"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $eq: [
                      "$$talentId",
                      "$talentId"
                    ]
                  }
                }
              },
              {
                $project: {
                  talentId: 0
                }
              }
            ]
          }
        },
        {
          $project: {
            userId: 0
          }
        }
      ]
    }
  }
])

分开的查询中的部分进行解释,可以按顺序组合它们。

工作游乐场:https://mongoplayground.net/p/poibvfumvt4