I have written a query in mongodb using aggregation which is working fine but somehow it is not working properly in my python code with pymongo. Please advice how to rectify.
Mongo query:
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 } }
])
In python code:
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)
the query result from pymongo is coming incorrect but in mongoDB it is correct