如何在Python中翻译Mongodb聚合命令?

时间:2017-03-15 18:28:55

标签: python mongodb aggregation-framework

我在mongo CLI中运行以下命令,以识别具有相同创建日期的集合中的2个或更多文档。

db.collection_name.aggregate({$group: { _id: "$date", count: { $sum: 1 }, docs:{ $push : "$_id" }}}, { $match: { count: { $gt: 1 } } })

但是,当我尝试从包含以下行的Python脚本运行它时,我收到错误。

collection = db[collections_name]
collection.aggregate({"$group": { '_id': "$date", 'count': { "$sum": 1 }, 'docs':{ "$push" : "$_id" }}}, { "$match": { 'count': { "$gt": 1 } } })

无法理解我做错了什么。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

管道操作必须作为python列表传递

collection.aggregate([{"$group": { '_id': "$date", 'count': { "$sum": 1 }, 'docs':{ "$push" : "$_id" }}}, { "$match": { 'count': { "$gt": 1 } } }])