mongodb - 如何在mongo-driver-go中更新指定数量的文档,就像mysql`update table set filed = dxx limit 50`

标签 mongodb go

像mysql一样如何在Mongo中更新指定数量的文档

update table set filed = dxx limit 50

使用此go版本mongo-driver
https://godoc.org/go.mongodb.org/mongo-driver/mongo

最佳答案

使用Mongo DB Shell,我们可以使用以下查询:
db.collection.find().limit(NUMBER_OF_ITEMS_YOU_WANT_TO_UPDATE).forEach( function (element) { element.newField = newData; db.collection.save(element); });
要使用mongo go驱动程序实现相同的操作,

1.使用find选项中的限制使用Collection.find()方法获取所需记录
2.使用查找查询的光标并在[] mongo.WriteModel中追加所需的更新查询
3.使用[] mongo.WriteModel Slice作为输入执行collection.BulkWrite()方法,并使用返回的BulkWriteResult。

关于mongodb - 如何在mongo-driver-go中更新指定数量的文档,就像mysql`update table set filed = dxx limit 50`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449568/

相关文章:

html - Golang 在 Revel 中将字符串渲染为 html

mongodb - 无法理解 ubuntu 14.04 中的 Mongodb upstart ('/etc/init.d/mongod' ) 脚本

java - 如何获取批量操作中插入/删除/更新失败的文档的数组 _ids?

mongodb - 将 robomongo 连接到 mongoDB docker 容器

go - 如何使用golang在请求 header 中保持关键大小写敏感?

sql - 将数组类型的数据库字段扫描到 Golang 中的数组/slice 中

go - 如何根据参数类型获取返回值?

python - 导入错误 : cannot import name 'binary_type'

ruby - 如何将 float 转换为整数?

go - 最小正float64值