我有一个MongoDB查询,如下所示:
data = db.collection.aggregate([{"$match":{"created_at":{"$gte":start,"$lt":end}}},{"$group":{"_id":"$stage","count":{"$sum":1}}},{"$match":{"count":{"$gt":m{u'count': 296, u'_id': u'10.57.72.93'}
结果如下:
{u'count': 230, u'_id': u'111.11.111.111'}
{u'count': 2240, u'_id': u'111.11.11.11'}
我正在尝试按“计数”列对输出进行排序:
data.sort('count', pymongo.DESCENDING)
...但是出现以下错误:
'CommandCursor' object has no attribute 'sort'
谁能解释这个错误的原因?
$sort
如汇总示例所示使用:
from bson.son import SON
data = db.collection.aggregate([
{"$match":{"created_at":{"$gte":start,"$lt":end}}},
{"$group":{"_id":"$stage","count":{"$sum":1}}},
{"$match":{"count": ... }},
{"$sort": SON([("count", -1)])} # <---
])
替代的一般解决方案:sorted
与自定义键功能一起使用:
data = db.collection.aggregate(...)
data = sorted(data, key=lambda x: x['count'], reverse=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句