Home  >  Q&A  >  body text

关于mongodb的aggregate()中$slice的使用

在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'

不知道是为什么,请高手看看,不胜感激!

PHP中文网PHP中文网2632 days ago662

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-02 09:20:00

    不知道你mongodb的版本是多少,据mongodb docs上显示,aggregation$slice是在mongodb 3.2之后才加入的。


    https://docs.mongodb.org/manual/reference/operator/aggregation/slice/

    reply
    0
  • Cancelreply