gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:37:17 35 4
gpt4 key购买 nike

像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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com