从一个mongo集合中删除在另一个集合中匹配的文档

时间:2016-04-25 20:03:56

标签: mongodb

这可能是一个noobie问题,但是,我如何(有效地)从一个mongodb集合中删除与另一个集合中找到的文档匹配的所有文档?例如,使用mongo shell,我们可以执行以下操作:

   db.getCollection('coll1').find({}).forEach( function(doc) {
     db.getCollection('coll2').remove( { name: doc.name, value: doc.value } );
   })

1 个答案:

答案 0 :(得分:0)

我建议使用$ in with remove通过传递名称数组或Id在$ in数组中删除。

 db.getCollection('coll1').find({}).forEach( function(doc) {
   
   // create idsArray from the doc data and pass same in $in
   
     db.getCollection('coll2').remove({ id: { $in: idsArray });
   })

我跳了它会有所帮助。 :)

相关问题