聚合中无效的运算符'$ size' [英] Invalid operator '$size' in aggregation

查看:90
本文介绍了聚合中无效的运算符'$ size'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下代码:

from pymongo import MongoClient
client = MongoClient('ipOfServer')
db = client.admin
db.authenticate('login', 'password',
source='admin_')
heh = list(db.events.aggregate(
    [
        {"$match": {"status": 'start'}},
        {"$group": {"_id": "$eventName", "players": {"$addToSet": "$uid"}}},
        {"$project": {"_id": 1, "Count": {"$size": "$players"}}}
    ]))
print(heh)

这适用于编写和测试它的原始程序员在测试时编码结果.但是,当我尝试运行它时,出现此错误:

and this is worked for the original programmer who wrote and tested it code result while testing. But when I try to run it I'm getting this error:

pymongo.errors.OperationFailure: exception: invalid operator '$size'

我使用带有崇高文本编辑器的mongo版本2.4.14和python 2.7.12.任何人都可以提出解决此问题的方法,将不胜感激.

I'm using mongo version 2.4.14 and python 2.7.12 with the sublime text editor. Could anyone suggest ways to solve this problem, it would be appreciated.

推荐答案

原因是因为

The reason is because the $size array aggregation operator is new in MongoDB 2.6 and you are actually running MongoDB 2.4.

我建议您将MongoDB服务器至少升级到3.0.但是,如果由于某种原因您不想立即升级,则需要 $unwind 玩家"数组和 $group 按"_id",然后使用 $sum 累加器运算符返回计数

I suggest you upgrade your MongoDB server to at least 3.0. But if for some reason you don't want to upgrade now, you will need to $unwind the "players" array and $group by "_id" then return the count using the $sum accumulator operator.

heh = list(db.events.aggregate(
    [
        {"$match": {"status": 'start'}},
        {"$group": {"_id": "$eventName", "players": {"$addToSet": "$uid"}}},
        {"$unwind": "$players"},
        {"$group": {"_id": "$_id", "Count": {"$sum": 1}}},
    ]))

这篇关于聚合中无效的运算符'$ size'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆