我正在尝试使用pymongo创建一个脚本来计算这样的几个事务:
{
"_id" : ObjectId("58437604a966aec46dfa249f"),
"transaction_id" : 282932121,
"transaction_serial_number" : "GtgT46A",
"transaction_date" : ISODate("2015-09-28T00:00:00Z"),
"card_type" : "MC",
"transaction_nominal" : 3500
}
...
我成功使用MongoDB直接聚合这些数据,以下是示例结果:
{
"_id" : {
"card_type" : "MC",
"date" : ISODate("2015-09-28T00:00:00Z")
},
"count" : 179011
}
但是,我没有使用pymongo聚合这些数据并得到错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "script_aggregate.py", line 84, in <module>
cursor = db.transaction.aggregate(match, proj1, proj2, group);
TypeError: aggregate() takes exactly 2 arguments (5 given)
这是我的剧本:
from datetime import datetime
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['mydb']
collection = db['transaction']
match={"$match" : {
"transaction_date" : { "$gt" : datetime(2013,1,1).isoformat() }
}
};
proj1={"$project" : {
"_id" : 0,
"transaction_date" : 1,
"card_type" : 1,
"h" : {
"$hour" : "$transaction_date"
},
"m" : {
"$minute" : "$transaction_date"
},
"s" : {
"$second" : "$transaction_date"
},
"ml" : {
"$millisecond" : "$transaction_date"
}
}
};
proj2={"$project" : {
"_id" : 0,
"card_type" : 1,
"transaction_date" : {
"$subtract" : [
"$transaction_date",
{
"$add" : [
"$ml",
{
"$multiply" : [
"$s",
1000
]
},
{
"$multiply" : [
"$m",
60,
1000
]
},
{
"$multiply" : [
"$h",
60,
60,
1000
]
}
]
}
]
}
}
};
group={"$group" : {
"_id" : {
"card_type" : "$card_type",
"date" : "$transaction_date"
},
"count" : {
"$sum" : 1
}
}
};
cursor = db.transaction.aggregate(match, proj1, proj2, group);
for document in cursor:
print(document);
我该怎么办?提前致谢
答案 0 :(得分:1)
查看aggregate
语法http://api.mongodb.com/python/current/examples/aggregation.html
您需要使用[]
作为参数。