Mongo 聚合框架无法按两个字段排序



我正在对 mongo 2.4.9 集合进行一些聚合,但我无法按两个字段对结果进行排序。这是我使用PyMongo进行的查询:

result = mongo_coll.aggregate([{"$match": {"_cls": "class1"},
                               {"$group": {"_id": {"currency": "$total.currency",
                                                   "v_id": "$v_id"},
                                           "total": {"$sum": "$total.amount"},
                                           "count": {"$sum": 1}}},
                               {"$sort": {"_id.currency": 1, "total": -1}}])

我的结果按"总计"排序:-1

如果我将最后一行替换为以下内容:

                               {"$sort": {"total": -1, "_id.currency": 1}}])

它仍然按"总计"排序:-1

如果我将其替换为以下内容:

                               {"$sort": {"_id.currency": 1}}])

它按货币排序。

但是我无法按照我想要的方式对其进行排序,这意味着首先按货币排序,然后按总数排序......(正如预期的那样,其他结果看起来不错)。有人有线索吗?

最好,提前感谢!

更新:这是一个示例文档:

{
  "_id": { "$oid" : "533d0a3b830f783478a75aa1" },
  "_cls": "class1",
  "v_id": 6813,
  "total": {
    "amount": 680,
    "currency": "EUR",
    "exp": -2
  }
}

我实际上可以找到Python发生这种情况的原因,这要归功于MongoDB用户Google组中Bernie的回答:

Python dict是无序的,这对于进行排序非常明智:-p

这就是为什么可以将参数作为BSON.SON dictOrderedDict给出以使其更具pythonic!

这是我使用的解决方案:

    from collections import OrderedDict
    sort_dict = OrderedDict()
    sort_dict['_id.currency'] = 1
    sort_dict['total'] = -1

然后

{"$sort": sort_dict}

编辑来自谷歌用户组中响应的链接...

如果您希望首先按货币进行排序,则应将排序更改为 - {"$sort": {"_id.currency": 1, "total": -1}} 。排序顺序由指定键的顺序驱动。

这些是我创建的示例文档 -

{ "_id" : ObjectId("533dc9d272337e43d14600f7"), "_cls" : "class1", "v_id" : 6813, "total" : { "amount" : 680, "currency" : "EUR", "exp" : -2 } }
{ "_id" : ObjectId("533dc9d972337e43d14600f8"), "_cls" : "class1", "v_id" : 6813, "total" : { "amount" : 690, "currency" : "EUR", "exp" : -2 } }
{ "_id" : ObjectId("533dc9de72337e43d14600f9"), "_cls" : "class1", "v_id" : 6813, "total" : { "amount" : 690, "currency" : "USD", "exp" : -2 } }
{ "_id" : ObjectId("533dc9e672337e43d14600fa"), "_cls" : "class1", "v_id" : 6813, "total" : { "amount" : 680, "currency" : "USD", "exp" : -2 } }
{ "_id" : ObjectId("533dcd0172337e43d14600fb"), "_cls" : "class1", "v_id" : 6813, "total" : { "amount" : 2000, "currency" : "CHE", "exp" : -2 } }
{ "_id" : ObjectId("533dcdfb72337e43d14600fc"), "_cls" : "class1", "v_id" : 6814, "total" : { "amount" : 2000, "currency" : "CHE", "exp" : -2 } }
{ "_id" : ObjectId("533dce1572337e43d14600fd"), "_cls" : "class1", "v_id" : 6815, "total" : { "amount" : 1000, "currency" : "CHE", "exp" : -2 } }

对于查询 - db.sample4.aggregate([{"$match": {"_cls": "class1"}},{$group:{"_id":{"currency":"$total.currency","v_id":"$v_id"},"total":{$sum:"$total.amount"}}},{"$sort": {"_id.currency": 1, "total": -1}}])输出为 -

{
    "result" : [
        {
            "_id" : {
                "currency" : "CHE",
                "v_id" : 6814
            },
            "total" : 2000
        },
        {
            "_id" : {
                "currency" : "CHE",
                "v_id" : 6813
            },
            "total" : 2000
        },
        {
            "_id" : {
                "currency" : "CHE",
                "v_id" : 6815
            },
            "total" : 1000
        },
        {
            "_id" : {
                "currency" : "EUR",
                "v_id" : 6813
            },
            "total" : 1370
        },
        {
            "_id" : {
                "currency" : "USD",
                "v_id" : 6813
            },
            "total" : 1370
        }
    ],
    "ok" : 1
}

对于查询db.sample4.aggregate([{"$match": {"_cls": "class1"}},{$group:{"_id":{"currency":"$total.currency","v_id":"$v_id"},"total":{$sum:"$total.amount"}}},{"$sort": {"_id.currency": 1, "total": 1}}])输出为 -

{
    "result" : [
        {
            "_id" : {
                "currency" : "CHE",
                "v_id" : 6815
            },
            "total" : 1000
        },
        {
            "_id" : {
                "currency" : "CHE",
                "v_id" : 6814
            },
            "total" : 2000
        },
        {
            "_id" : {
                "currency" : "CHE",
                "v_id" : 6813
            },
            "total" : 2000
        },
        {
            "_id" : {
                "currency" : "EUR",
                "v_id" : 6813
            },
            "total" : 1370
        },
        {
            "_id" : {
                "currency" : "USD",
                "v_id" : 6813
            },
            "total" : 1370
        }
    ],
    "ok" : 1
}

相关内容

  • 没有找到相关文章

最新更新