Invalid operator '$size' in aggregation

前端 未结 1 526
执念已碎
执念已碎 2021-01-25 22:14

I got the following piece of code:

from pymongo import MongoClient
client = MongoClient(\'ipOfServer\')
db = client.admin
db.authenticate(\'login\', \'password\         


        
相关标签:
1条回答
  • 2021-01-25 22:54

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

    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}}},
        ]))
    
    0 讨论(0)
提交回复
热议问题