我写了一个使用聚合的mongodb查询工作正常,但不知何故它不能在我的Python代码与pymongo正常工作。请咨询如何纠正。PyMongo关于聚合查询
蒙戈查询:
db.flights.aggregate([
{ $match: { origin:"ATL", dest:"BOS",dayofweek: 3} },
{ $group: {
_id: {
origin:"$origin",
destination: "$dest"
},
Failure: { $sum: { $cond : [{ $eq : ["$cancelled", 1]}, 1, 0]} },
Success: { $sum: { $cond : [{ $eq : ["$cancelled", 0]}, 1, 0]} },
Total: { $sum: 1 }
} },
{$project:{Failure:1,Success:1, Total:1, FailPercent: { $divide: [ "$Failure", "$Total" ]}}},
{ $sort: { "_id.origin": 1, "_id.destination": 1 } }
])
在Python代码:
client = MongoClient("localhost", 27017)
connect = client["database"]["collection"]
pipe2 = [ { '$match': { 'origin':"ATL", 'dest':"BOS",'dayofweek': 3} },
{ '$group': {
'_id': {
'origin':"$origin",
'destination': "$dest"
},
'Failure': { '$sum': { '$cond' : [{ '$eq' : ["$cancelled", 1]}, 1, 0]} },
'Success': { '$sum': { '$cond' : [{ '$eq' : ["$cancelled", 0]}, 1, 0]} },
'Total': { '$sum': 1 }
} },{'$project':{'Failure':1,'Success':1, 'Total':1, 'FailPercent': { '$divide': [ "$Failure", "$Total" ]}}},
{ '$sort': SON([("_id.origin", 1), ("_id.destination", 1)]) }
]
result = connect.aggregate(pipeline=pipe2)
从pymongo查询结果来了不正确,但在MongoDB中它是正确的
什么是结果你回来了? – 2014-11-24 09:35:37