Mongodb Mongo聚合框架无法按两个字段排序?
我正在对mongo 2.4.9集合进行聚合,但无法按两个字段对结果进行排序。以下是我使用PyMongo进行的查询:Mongodb Mongo聚合框架无法按两个字段排序?,mongodb,sorting,pymongo,aggregation-framework,Mongodb,Sorting,Pymongo,Aggregation Framework,我正在对mongo 2.4.9集合进行聚合,但无法按两个字段对结果进行排序。以下是我使用PyMongo进行的查询: result = mongo_coll.aggregate([{"$match": {"_cls": "class1"}, {"$group": {"_id": {"currency": "$total.currency",
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}}])
{"$sort": {"_id.currency": 1}}])
它仍然按“总数”排序:-1
如果我将其替换为以下内容:
{"$sort": {"total": -1, "_id.currency": 1}}])
{"$sort": {"_id.currency": 1}}])
它按货币排序
但我不能按我想要的方式来排序,也就是说先按货币排序,然后按总额排序。。。(其他方面的结果看起来不错,正如预期的那样)。有人有线索吗
祝你万事如意,谢谢你
更新:这是一个示例文档:
{
"_id": { "$oid" : "533d0a3b830f783478a75aa1" },
"_cls": "class1",
"v_id": 6813,
"total": {
"amount": 680,
"currency": "EUR",
"exp": -2
}
}
如果希望首先按货币进行排序,则应将排序更改为-
{“$sort”:{“\u 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”:{“\u cls”:“class1”},{$group:{“\u id”:{“currency”:{“currency”:“$total.currency”,“v\u id”:“$v\u id”},“total”:{$sum:$total.amount”},{“$sort”:{“{“\u 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
}
{
"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
}
对于查询db.sample4.aggregate([{“$match”:{“\u cls”:“class1”},{$group:{“\u id”:{“currency”:“$total.currency”,“v\u id”:“$v\u id”}”,total:{$sum:$total.amount”},{“$sort”:{“\u 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
}
{
"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
}
感谢MongoDB用户Google Group中的Bernie的回答,我可以找到Python出现这种情况的原因: Python
dict
是无序的,这对于排序是非常明智的:-p
这就是为什么可以将参数设置为BSON.SON dict
或OrderedDict
以使其更具python风格
以下是我使用的解决方案:
from collections import OrderedDict
sort_dict = OrderedDict()
sort_dict['_id.currency'] = 1
sort_dict['total'] = -1
然后
{"$sort": sort_dict}
编辑
google用户组中的链接…货币的数据类型是什么?您可以发布您的收藏中的样本文档吗?货币是一个StringField,我将用样本文档更新问题。您可以发布一些样本输出,其中文档的排序与您期望的顺序不同(以及您在此查询中指定的排序顺序)。在快速测试中,我无法重现MongoDB 2.4.9的问题。一种可能是你有一些总数,可能是字符串而不是数字。我完全同意你的看法!我也是这样开始的!问题是,在我的收藏中,使用这种排序方式,它不是按这种方式排序的,而是只按{“total”:-1}排序!!!这就是为什么我怀疑某些错误。我在匹配了几百万个条目之后,对几千个条目的聚合结果进行了排序