2014-11-22 149 views
0

我写了一个使用聚合的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中它是正确的

+0

什么是结果你回来了? – 2014-11-24 09:35:37

回答

1

的 “管道” 变量似乎没有必要。没有看到你的错误,并假设你的数据库连接是好的

这条线:

result = connect.aggregate(pipeline=pipe2) 

应该仅仅是:

result = connect.aggregate(pipe2) 

从所给的信息复制您的收藏后,这个工作为了我。下面是完整的代码(我的连接看起来比你有点不同以及)

收藏:

{ '_id':1, '出身': 'ATL', 'DEST': 'BOS' ,'dayofweek':3,'取消':0}

{'_id':2,'origin':'ATL','dest':'BOS','dayofweek':3,'cancelled': 0}

{ '_id':3 '原点': 'ATL', 'DEST': 'BOS', '星期几':3 '取消':1}

代码:

import pymongo 
from bson.son import SON 

connection_string = 'mongodb://localhost' 
connection = pymongo.MongoClient(connection_string) 
database = connection.myDatabase 

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 = database.myCollection.aggregate(pipe2) 
print(result) 

输出:

{u'ok ':1.0,u'result':[{u'Failure ':1,u'_id':{U”起源':u'ATL',u'destination':u'BOS'},u'FailPercent':0.333333333333,u'Success':2,u'Total':3}]}

+0

您好BMan..thnx您的答复..所以我明白了这一点,根据你的不必要的参数是导致错误?我会尝试n分享结果。和DB的连接是好的,我检查了第一件事。 :) – miku 2014-11-26 09:23:25

+1

@miku - 我的第一印象是,不必要的参数可能会导致你的错误。但是,我给了我用来获得成功输出的完整代码,以防事件不成功。如果您仍然遇到问题,请发布您收到的无效输出或错误。 – BMan 2014-12-01 15:50:04