簡體   English   中英

mongo 中的聚合查詢有效,Pymongo 中無效

[英]Aggregate query in mongo works, does not in Pymongo

我遇到了一個問題。 我嘗試通過“COL”數組之外的 LOC 標識符查詢此文檔以獲取金額和分組的總和。

{
"_id" : ObjectId("57506d74c469888f0d631be6"),
"LOC" : "User001",
"COL" : [ 
    {
        "date" : "25/03/2016",
        "number" : "Folio009",
        "amount" : 100
    }, 
    {
        "date" : "25/04/2016",
        "number" : "Folio010",
        "amount" : 100
    }

] }

此命令在 mongo 中有效,但我無法使用 Pymongo 包使其在 Python 中運行:

Mongo 查詢(工作)

db.perfiles.aggregate({"$unwind": "$COL"},
{ "$group": { _id: "$LOC", "sum" : {"$sum" : "$COL.amount" }}})

Pymongo(不工作)

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))

有什么建議可以在 Pymongo 中查詢相同的查詢? 謝謝!

我假設您在 Python 中有一個到 MongoDB 的有效連接。
以下代碼片段將在result.返回一個 MongoDB 游標result.

pipeline = [
    {"$unwind": "$COL"},
    {"$group": {"_id": "$LOC", "sum": {"$sum": "$COL.amount"}}}
]

cursor = collection.aggregate(pipeline)

現在您可以將cursor轉換為列表

result = list(cursor)

如果您打印結果的值,您將獲得與 Shell 查詢完全相同的結果。

[{u'sum': 200.0, u'_id': u'User001'}]

更新

我看到你在 python 代碼中調用aggregate函數作為db.docs.aggregate(pipeline) 您需要將其稱為docs.aggregate...不帶db 請參閱上面的示例。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM