【问题标题】:PyMongo query on AggregationPyMongo 对聚合的查询
【发布时间】:2014-11-22 07:16:25
【问题描述】:

我使用聚合在 mongodb 中编写了一个查询,该查询运行良好,但不知何故,它在我的 python 代码中使用 pymongo 无法正常运行。请指教如何纠正。

Mongo 查询:

 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 中是正确的

【问题讨论】:

  • 你得到的结果是什么?

标签: mongodb python-2.7 pymongo


【解决方案1】:

“管道”变量似乎没有必要。没有看到您的错误,并假设您与数据库的连接正常

这一行:

result = connect.aggregate(pipeline=pipe2)

应该是:

result = connect.aggregate(pipe2)

根据提供的信息复制您的收藏后,这对我有用。这是完整的代码(我的连接看起来也和你的有点不同)

收藏:

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

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

{ '_id' : 3, 'origin' : 'ATL', 'dest' : 'BOS', 'dayofweek' : 3, 'cancelled' : 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'origin': u'ATL', u'destination': u'BOS' }, u'FailPercent': 0.333333333333, u'Success': 2, u'Total': 3}]}

【讨论】:

  • 嗨 BMan..thnx 为您的回复..所以我很清楚这一点,根据您的说法,不必要的参数导致了错误?我会尝试分享结果。与数据库的连接很好,我首先检查了它。 :)
  • @miku - 我的第一印象是不必要的参数可能会导致您的错误。但是,我给出了我用来获得成功输出的完整代码,以防万一。如果您仍然遇到问题,请发布您收到的无效输出或错误。
猜你喜欢
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 2020-01-15
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
相关资源
最近更新 更多