我正在使用golang将批量数据写入monogdb。假设我们有以下数据
[
{
id:1,
name:"abc"
}
{
id:2,
name:"cde"
}
....upto 1000
]
为了保存此数据,我正在使用以下代码对此进行批量写入操作
mongoSession, ctx := config.ConnectDb("myFirstDatabase")
defer mongoSession.Disconnect(ctx)
var operations []mongo.WriteModel
operationA := mongo.NewInsertOneModel()
getCollection := mongoSession.Database("myFirstDatabase").Collection("transactions")
for k, v := range transaction {
operationA.SetDocument(transaction[k])
operations = append(operations, operationA)
fmt.Println(operationA)
}
bulkOption := options.BulkWriteOptions{}
// bulkOption.SetOrdered(true)
_, err = getCollection.BulkWrite(ctx, operations, &bulkOption)
operations是 for 循环的类型,我将单个文档附加到操作中,但另一方面,当我在 mongodb 中验证所有文档都在那里时,只有最后一个文档在 mongodb 中写入了 1000 次。请让我知道上面的代码片段中的哪个行代码是错误的。[]mongo.WriteModel
米脂
慕仙森
随时随地看视频慕课网APP
相关分类