在mongodb中使用aggregate(函數作聚合),pipe是這樣寫的:
pipe = [
{'$match':{'time':'03.02'}},
{'$group':{'_id':{'$slice':['$address',0,1]},'counts':{'$sum':1}}},
{'$sort':{'counts':-1}}
]
address欄位是list型,然後想取出address中第一項,出現一個就加一,但是總是報錯:
OperationFailure: command SON([('aggregate', u'item_info_3'), ('pipeline', [{'$match': {'time': '03.02'}}, {'$group': {' counts': {'$sum': 1}, '_id': {'$slice': ['$address', 0, 1]}}}, {'$sort': {'counts': -1} }]), ('cursor', {})]) on namespace GanjiData.$cmd failed: exception: invalid operator '$slice'
不知道是為什麼,請高手看看,不勝感激!
不知道你
mongodb
的版本是多少,据mongodb docs
上显示,aggregation
的$slice
是在mongodb 3.2
之後才加入的。見
https://docs.mongodb.org/manual/reference/operator/aggregation/slice/