[英]Aggregate query in mongo works, does not in Pymongo
I encountered a problem.我遇到了一个问题。 I try to query this document to obtain the sum the amount and group by the LOC identifier that is outside the "COL" array.
我尝试通过“COL”数组之外的 LOC 标识符查询此文档以获取金额和分组的总和。
{
"_id" : ObjectId("57506d74c469888f0d631be6"),
"LOC" : "User001",
"COL" : [
{
"date" : "25/03/2016",
"number" : "Folio009",
"amount" : 100
},
{
"date" : "25/04/2016",
"number" : "Folio010",
"amount" : 100
}
] } ] }
This command works in mongo but I cannot make it work in Python with the Pymongo package:此命令在 mongo 中有效,但我无法使用 Pymongo 包使其在 Python 中运行:
db.perfiles.aggregate({"$unwind": "$COL"},
{ "$group": { _id: "$LOC", "sum" : {"$sum" : "$COL.amount" }}})
from pymongo import MongoClient
client = MongoClient()
db = client['temporal']
docs = db.perfiles
pipeline = [{"$unwind": "$COL"},
{"$group": {"_id": "$LOC", "count": {"$sum": "$COL.amount"}}}
]
list(db.docs.aggregate(pipeline))
Any suggestion to query this same query but in Pymongo?有什么建议可以在 Pymongo 中查询相同的查询? Thanks!
谢谢!
I assume you have a valid connection to MongoDB in Python.我假设您在 Python 中有一个到 MongoDB 的有效连接。
The following code snippet will return a MongoDB cursor in result.
以下代码片段将在
result.
返回一个 MongoDB 游标result.
pipeline = [
{"$unwind": "$COL"},
{"$group": {"_id": "$LOC", "sum": {"$sum": "$COL.amount"}}}
]
cursor = collection.aggregate(pipeline)
Now you can convert cursor
to list现在您可以将
cursor
转换为列表
result = list(cursor)
and if you print result's value, you'll get exactly the same result as in your Shell query.如果您打印结果的值,您将获得与 Shell 查询完全相同的结果。
[{u'sum': 200.0, u'_id': u'User001'}]
Update :更新:
I see that you are calling the aggregate
function in python code as db.docs.aggregate(pipeline)
.我看到你在 python 代码中调用
aggregate
函数作为db.docs.aggregate(pipeline)
。 You need to call it as docs.aggregate...
without db
.您需要将其称为
docs.aggregate...
不带db
。 See example above.请参阅上面的示例。
MongoDB Enterprise > db.test.aggregate([{$match:{name:'prasad'}},{$group : {_id : "$name", age : {$min : "$age"}}}]);
{ "_id" : "prasad", "age" : "20" }
MongoDB Enterprise > db.test.find()
{ "_id" : ObjectId("5890543bce1477899c6f05e8"), "name" : "prasad", "age" : "22" }
{ "_id" : ObjectId("5890543fce1477899c6f05e9"), "name" : "prasad", "age" : "21" }
{ "_id" : ObjectId("58905443ce1477899c6f05ea"), "name" : "prasad", "age" : "20" }
{ "_id" : ObjectId("5890544bce1477899c6f05eb"), "name" : "durga", "age" : "20" }
{ "_id" : ObjectId("58905451ce1477899c6f05ec"), "name" : "durga", "age" : "21" }
{ "_id" : ObjectId("58905454ce1477899c6f05ed"), "name" : "durga", "age" : "22" }
MongoDB Enterprise >
############code
import pymongo
from pymongo import MongoClient
client=MongoClient("localhost:27017")
db=client.prasad #####prasad is dbname, test is collection name
nameVar='prasad'
aggregation_string=[{"$match":{"name":nameVar}},{"$group" : {"_id" : "$name", "age" : {"$min" : "$age"}}}]
x=db.test.aggregate(aggregation_string)
print x
for r in x:
min_age=r.items()[0]
print(min_age[1]) #######output: 20
you are in a right track but add one more statement it will be fine.
from pymongo import MongoClient
client = MongoClient()
db = client['temporal']
docs = db.perfiles
pipeline = [{"$unwind": "$COL"},
{"$group": {"_id": "$LOC", "count": {"$sum": "$COL.amount"}}}
]
result = list(db.docs.aggregate(pipeline))
for i in result:
sum += i['sum']
print(sum)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.