如何 BulkWrite\UpdateMany 使用 MongoDB 的 Go 驱动程序
How to BulkWrite\UpdateMany with Go driver for MongoDB
我正在从 mgo 驱动程序迁移,我的函数如下所示:
queue := collection.Bulk()
for j := range changes {
..
queue.Update(doc, update)
}
saveResult, err := queue.Run()
这使得一些 $push
和 $set
更新循环中的单个文档。
我应该如何使用官方驱动程序执行此操作?是 collection.BulkWrite()
还是 collection.UpdateMany()
?文档非常模糊,我不知道如何使用它们以及有什么区别。任何帮助将不胜感激。
对于您的用例,您将使用 collection.BulkWrite
。您可以在存储库的 examples directory 中找到如何使用 go-mongo-driver
的示例。
collection.UpdateMany()
将使用相同的更新过滤器和修改更新 collection 中的多个文档。 mongo shell 等价物的 docs 中有更多文档。示例:
result, err := coll.UpdateMany(
context.Background(),
bson.NewDocument(
bson.EC.SubDocumentFromElements("qty",
bson.EC.Int32("$lt", 50),
),
),
bson.NewDocument(
bson.EC.SubDocumentFromElements("$set",
bson.EC.String("size.uom", "cm"),
bson.EC.String("status", "P"),
),
bson.EC.SubDocumentFromElements("$currentDate",
bson.EC.Boolean("lastModified", true),
),
),
)
collection.BulkWrite()
将在几天前执行一组 bulk write operations. The BulkWrite API was only introduced 去 driver。示例很少,但是您可以随时检查测试
文件。示例:
var operations []mongo.WriteModel
operation := mongo.NewUpdateOneModel()
operation.Filter(bson.NewDocument(
bson.EC.SubDocumentFromElements("qty",
bson.EC.Int32("$lt", 50),
),
))
operation.Update(bson.NewDocument(
bson.EC.SubDocumentFromElements("$set",
bson.EC.String("size.uom", "cm"),
bson.EC.String("status", "P"),
),
bson.EC.SubDocumentFromElements("$currentDate",
bson.EC.Boolean("lastModified", true),
),
))
operations = append(operations, operation)
result, err := coll.BulkWrite(
context.Background(),
operations,
)
我正在从 mgo 驱动程序迁移,我的函数如下所示:
queue := collection.Bulk()
for j := range changes {
..
queue.Update(doc, update)
}
saveResult, err := queue.Run()
这使得一些 $push
和 $set
更新循环中的单个文档。
我应该如何使用官方驱动程序执行此操作?是 collection.BulkWrite()
还是 collection.UpdateMany()
?文档非常模糊,我不知道如何使用它们以及有什么区别。任何帮助将不胜感激。
对于您的用例,您将使用 collection.BulkWrite
。您可以在存储库的 examples directory 中找到如何使用 go-mongo-driver
的示例。
collection.UpdateMany()
将使用相同的更新过滤器和修改更新 collection 中的多个文档。 mongo shell 等价物的 docs 中有更多文档。示例:
result, err := coll.UpdateMany(
context.Background(),
bson.NewDocument(
bson.EC.SubDocumentFromElements("qty",
bson.EC.Int32("$lt", 50),
),
),
bson.NewDocument(
bson.EC.SubDocumentFromElements("$set",
bson.EC.String("size.uom", "cm"),
bson.EC.String("status", "P"),
),
bson.EC.SubDocumentFromElements("$currentDate",
bson.EC.Boolean("lastModified", true),
),
),
)
collection.BulkWrite()
将在几天前执行一组 bulk write operations. The BulkWrite API was only introduced 去 driver。示例很少,但是您可以随时检查测试
文件。示例:
var operations []mongo.WriteModel
operation := mongo.NewUpdateOneModel()
operation.Filter(bson.NewDocument(
bson.EC.SubDocumentFromElements("qty",
bson.EC.Int32("$lt", 50),
),
))
operation.Update(bson.NewDocument(
bson.EC.SubDocumentFromElements("$set",
bson.EC.String("size.uom", "cm"),
bson.EC.String("status", "P"),
),
bson.EC.SubDocumentFromElements("$currentDate",
bson.EC.Boolean("lastModified", true),
),
))
operations = append(operations, operation)
result, err := coll.BulkWrite(
context.Background(),
operations,
)