我正在使用pymongo。我有一个要根据另一个集合中的值更新字段的集合。这是收藏集中的文件1。
{ _id: ObjectId("5fef7a23d0bdc785d4fc94e7"),
path: 'path1.png',
type: 'negative',
xmin: NaN,
ymin: NaN,
xmax: NaN,
ymax: NaN}
从collection2中:
{ _id: ObjectId("5fef7a24d0bdc785d4fc94e8"),
path: 'path1.png',
xmin: 200,
ymin: 200,
xmax: 300,
ymax: 300}
如何更新集合1,以使示例文档如下所示:
{ _id: ObjectId("5fef7a23d0bdc785d4fc94e7"),
path: 'path1.png',
type: 'negative,
xmin: 200,
ymin: 200,
xmax: 300,
ymax: 300}
将collection2提取到dict
变量中并用于$set
更新collection1,例如
for doc in db.collection2.find({}, {'_id': 0}):
db.collection1.update_one({'path': doc.get('path')}, {'$set': doc})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句