首页 > 解决方案 > 在 Mongo 聚合中对多个对象数组进行排序

问题描述

{
    "_id" : 1,
    "name" : "Ashwanth Madhav",
    "scores" : [
            {
                    "type" : "exam",
                    "score" : 45
            },
            {
                    "type" : "quiz",
                    "score" : 34
            },
            {
                    "type" : "work",
                    "score" : 99
            },
            {
                    "type" : "work",
                    "score" : 38
            }
    ],
  "job_type" : [
            {
                    "type" : "full_time",
                    "activity" :87
            },
            {
                    "type" : "part_time",
                    "activity" : 78
            },
            {
                    "type" : "full_time",
                    "activity" : 71.76133439165544
            }
    ]
}

使用$addToSet排序列表时,会变得未排序。所以我用$push. 但它会导致数据重复。

我需要对 SCORES 和 JOB_TYPE 进行排序。现在,我已经完成了,$addToSet首先使用,然后是 " $unwind" score-> " $sort" -> " $group" with $push。在“ $unwind”作业类型->“ $sort”->“ $group”之后加上$push.

有没有更好的办法?提前致谢。

标签: databasemongodbmongoosemongodb-queryaggregate-functions

解决方案


推荐阅读