title: PyMongo query on Aggregation
body: <p>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.</p>
  
  <p>Mongo query:</p>
  
  <pre><code> 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 } } 
      ])
  </code></pre>
  
  <p>In python code:</p>
  
  <pre><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)
  </code></pre>
  
  <p>the query result from pymongo is coming incorrect but in mongoDB it is correct</p>