我正在处理 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)"}}]}
});
查询在 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)"}}]}
})
但是查询出现错误。
TypeError: group() takes at least 5 arguments (2 given)
我尝试通过以下网站(URL)中给出的方法解决错误
http://blog.pythonisito.com/2012/05/aggregation-in-mongodb-part-1.html
但同样的错误仍然存在。
最佳答案
group的语法在 PyMongo 中是不同的。 Javascript中参数对象中的key
、initial
等键在Python中都是关键字参数:
db.BH.group(key = , initial = , reduce = , cond = )
关于python - 如何在 python 中为 db.collection.group() 编写 mongodb 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29039298/