如何在python中为db.collection.group()写mongodb查询

abh*_*day 5 python mongodb pymongo mongodb-query

我正在处理MongoDB查询

db.BH.group({
"key": {
    "AccountId": true,
},
"initial": {
    "count": 0
},
"reduce": function(obj, prev) {
    if (true != null) if (true instanceof Array) prev.count += true.length;
    else prev.count++;
},
"cond":     
    {"$or":[{"Url":{"$regex":"(google)"}},{"Url":{"$regex":"(facebook)"}}]} 
}); 
Run Code Online (Sandbox Code Playgroud)

该查询在MongoDB Shell(Robomongo)中运行良好。

我已经为python编写了相同的查询。

db.BH.group({
"key": {
    "AccountId": True,
},
"initial": {
    "count": 0
},
"reduce": "function(obj, prev) {"
    "if (true != null) if (true instanceof Array) prev.count += true.length;"
    "else prev.count++;"
"}",
"cond": {"$or":[{"Url":{"$regex":"(google)"}},{"Url":{"$regex":"(facebook)"}}]} 
}) 
Run Code Online (Sandbox Code Playgroud)

但是查询出现错误。

TypeError: group() takes at least 5 arguments (2 given)
Run Code Online (Sandbox Code Playgroud)

我尝试通过以下网站(URL)中给出的方法解决错误

http://blog.pythonisito.com/2012/05/aggregation-in-mongodb-part-1.html

但是,同样的错误仍然存​​在。