我不明白如何使用mongo-go-driver
为MongoDB添加记录参数示例请求
subselect
如何在c.client.Database(MONGO_DATABASE).Collection(*packet.ID).InsertMany(nil, packet.Item, opt)
中指定必要的参数?
答案 0 :(得分:1)
基于mongo-go-driver的当前版本(0.0.16),可以设置一个write concern选项,如下例所示:
import(
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/core/writeconcern"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/mongodb/mongo-go-driver/mongo/collectionopt"
)
// Example document
document := bson.VC.DocumentFromElements(
bson.EC.SubDocumentFromElements(
"foo",
bson.EC.Int32("bar", 101),
),
)
// Set majority write concern
wMajority := writeconcern.New(writeconcern.WMajority())
database := client.Database("database")
collection := database.Collection("collection", collectionopt.WriteConcern(wMajority))
_, err = collection.InsertOne(context.Background(), document)
还可以使用W(int)
指定任意数量的mongod
实例。查看更多writeconcern/writeconcern.go