Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pymongo中forEach函数不起作用的Mongo聚合查询_Python_Mongodb_Pymongo_Pymongo 3.x - Fatal编程技术网

Python pymongo中forEach函数不起作用的Mongo聚合查询

Python pymongo中forEach函数不起作用的Mongo聚合查询,python,mongodb,pymongo,pymongo-3.x,Python,Mongodb,Pymongo,Pymongo 3.x,我有以下mongo聚合查询 db.configurations.aggregate([{$group:{_id:{model:"$model", vendor :"$vendor",access_level : "$access_level",config_data_type :"$config_data_type"}, dups:{$push:"$_id"}, count: {$sum: 1}}

我有以下mongo聚合查询

db.configurations.aggregate([{$group:{_id:{model:"$model", vendor :"$vendor",access_level : "$access_level",config_data_type :"$config_data_type"}, dups:{$push:"$_id"}, count: {$sum: 1}}},
{$match:{count: {$gt: 1}}}
]).forEach(function(doc){
  doc.dups.shift();
  db.configurations.remove({_id : {$in: doc.dups}});
});
对于pymongo,我写了一个相当于:


pipeline = [{"$group":{"_id":{"model":"$model", "vendor" :"$vendor","access_level" : "$access_level","config_data_type" :"$config_data_type"}, "dups":{"$push":"$_id"}, "count": {"$sum": 1}}},{"$match":{"count": {"$gt": 1}}}]

dest_col.aggregate(pipeline).forEach(bson.Code( '''
function(doc){
  doc.dups.shift();
  dest_col.remove({"_id ": {"$in": doc.dups}});
}'''));

这将导致以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'CommandCursor' object has no attribute 'forEach'

回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“CommandCursor”对象没有属性“forEach”

请更正我所做的任何语法错误。或者让我知道是否需要遵循任何其他格式才能使其工作

将聚合包装在
列表中,如下所示-因为聚合函数返回一个
游标
对象。另外,以前的解决方案不起作用,因为pythin没有类似于
forEach
的东西。您必须在
中执行
以进行迭代

result = list(dest_col.aggregate(pipeline))

for doc in result:
  bson.Code( '''
function(doc){
  doc.dups.shift();
  dest_col.remove({"_id ": {"$in": doc.dups}});
}''')

I am not python developer. Plz chk the code for syntax errors.

我得到这个错误->AttributeError:“list”对象没有属性“forEach”。谢谢你的投票。如果这个答案对你有用,你能接受吗。