使用MongoDB对文档中的数组进行排序

时间:2016-03-09 11:04:24

标签: mongodb mongodb-query aggregation-framework

我有来自mongoDb v 3.2的数据结构 我将尝试按月进行排序,并通过聚合框架

在统计数组中进行排序

来源数据:

   "monthStart" : "2015-11",
"monthEnd" : "2015-11",
"date" : ISODate("2016-03-09T09:06:58Z"),
"statistic" : [
    {
        "name" : "site1.com",
        "ga" : "99999",
        "data" : {
            "desktop" : {
                "users" : NumberLong(25),
                "pageviews" : NumberLong(56789),
            }
        }
    },
    {
        "name" : "site2.com",
        "ga" : "102",
        "data" : {
            "desktop" : {
                "users" : NumberLong(21),
                "pageviews" : NumberLong(9399393),
            }
        }
    },
    {
        "name" : "site3.com",
        "ga" : "103",
        "data" : {
            "desktop" : {
                "users" : NumberLong(103),
                "pageviews" : NumberLong(9399393),
            }
        }
    },
{
"monthStart" : "2015-12",
"monthEnd" : "2015-12",
"date" : ISODate("2016-03-09T09:08:39Z"),
"statistic" : [
    {
        "name" : "site1.com",
        "ga" : "9999",
        "data" : {
            "desktop" : {
                "users" : NumberLong(88),
                "pageviews" : NumberLong(83838),
            },
        }
    },
    {
        "name" : "site2.com",
        "ga" : "8888",
        "data" : {
            "desktop" : {
                "users" : NumberLong(75),
                "pageviews" : NumberLong(77777777),
            },

        }
    },
    {
        "name" : "site3.com",
        "ga" : "103",
        "data" : {
            "desktop" : {
                "users" : NumberLong(25),
                "pageviews" : NumberLong(9399393),
            }
        }
    },
    }
]

如何在数组统计中进行结构排序?

预期结果:

    "monthStart" : "2015-11",
"monthEnd" : "2015-11",
"date" : ISODate("2016-03-09T09:06:58Z"),
"statistic" : [
    {
        "name" : "site3.com",
        "ga" : "103",
        "data" : {
            "desktop" : {
                "users" : NumberLong(103),
                "pageviews" : NumberLong(9399393),
            }
        }
    },
            {
        "name" : "site2.com",
        "ga" : "102",
        "data" : {
            "desktop" : {
                "users" : NumberLong(21),
                "pageviews" : NumberLong(9399393),
            }
        }
    },
    {
        "name" : "site1.com",
        "ga" : "99999",
        "data" : {
            "desktop" : {
                "users" : NumberLong(25),
                "pageviews" : NumberLong(56789),
            }
        }
    },

{
"monthStart" : "2015-12",
"monthEnd" : "2015-12",
"date" : ISODate("2016-03-09T09:08:39Z"),
"statistic" : [
        {
        "name" : "site3.com",
        "ga" : "103",
        "data" : {
            "desktop" : {
                "users" : NumberLong(201),
                "pageviews" : NumberLong(9399393),
            }
        }
    },
    {
        "name" : "site1.com",
        "ga" : "9999",
        "data" : {
            "desktop" : {
                "users" : NumberLong(88),
                "pageviews" : NumberLong(83838),
            },
        }
    },
    {
        "name" : "site2.com",
        "ga" : "8888",
        "data" : {
            "desktop" : {
                "users" : NumberLong(75),
                "pageviews" : NumberLong(77777777),
            },

        }
    },

    }
]

我试过make,但它没有用:

     db.TrafficStatistic.aggregate([
{ "$unwind": "$statistic" },
{ "$sort": { "statistic.data.desktop.users": 1 } } ])

1 个答案:

答案 0 :(得分:3)

我看到了问题。人们正在寻找并找到这个stackoverflow答案:

  

how to sort array inside collection record in mongoDB

这是错误的,因为它永远不会“重建”数组。

您使用$group$push执行此操作,并且由于您正在进行分组,因此您需要$first来填写所需文档中的其他字段:

db.TrafficStatistic.aggregate([
   { "$unwind": "$statistic" },
   { "$sort": { "_id": 1, "statistic.data.desktop.users": 1 } },
   { "$group": {
       "_id": "$_id",
       "monthStart" : { "$first": "$monthStart" },
       "monthEnd" : { "$first": "$monthEnd" },
       "date" : { "$first": "$date" },
       "statistic": { "$push": "$statistic" }
   }}
])

另请注意,$sort适用于"_id"和要排序的其他字段。这样就可以按文档应用排序,并且当文档详细信息在$group中重新组合在一起时非常重要。

现在文档看起来和它一样,但这次是对数组成员进行排序。