嗨,我该如何在pymongo中使用foreach()
我有一些集合
。我想用汇总
代码搜索一些条件和数据。然后在pymongo
中使用foreach代码更新结果。是否可以? 例如,我写一个代码:
db.users.find({age: {$gt: 18}}).forEach( function(myDoc) {db.users.updateOne({_id: myDoc._id }, {$set: {type: "older"}} );
我想写这篇文章是在Pymongo中。
I have some collection
. I want to search with some condition and data with aggregate
code. Then update result with forEach code in pymongo
. Is it possible?
For example i write a code:
db.users.find({age: {$gt: 18}}).forEach( function(myDoc) {db.users.updateOne({_id: myDoc._id }, {$set: {type: "older"}} );
I want to write this is in pymongo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看您的代码,如果要在与某个条件匹配的所有记录上设置字段#pymongo.collection.collection.update_many“ rel =“ nofollow noreferrer”>
update_many()
而无需使用for for循环。Looking at your code, if you want to set a field on all records matching a certain criteria, you can just use
update_many()
without the need for a for loop.