0
我想用pymongo來統計匹配記錄。 我已經看過了文檔:https://docs.mongodb.com/manual/reference/operator/aggregation/group/#group-by-month-day-and-yearpymongo的總數 - 記錄數
我知道我應該這樣做:
db.foo_baz.aggregate([{'$group': {'_id': '$baz_id', 'count': {'$sum': 1}}}])
不過,我得到這個錯誤:
File "/usr/lib/python3.6/site-packages/mongomock/collection.py", line 1531, in aggregate
from_field = key.replace('$', '')
AttributeError: 'int' object has no attribute 'replace'
添加評論
我會認爲pymongo會評估它到這個mongodb聲明:
db.foo_baz.aggregate({{$group: {_id: '$baz_id', count: {$sum: 1}}}])
這給了我從mongodb控制檯運行時的預期結果,它是:{"_id" : NumberInt("1234"), "count" : 1}
在我的情況。
我在誤解什麼?我使用的是pymongo 3.4和python 3.6以及mongomock 3.7。
我也試圖與"1"
更換1
它不會崩潰,而是給出了一個不正確的結果:({"_id" : NumberInt("1234"), "count" : NumberInt("0")}
)