【发布时间】:2021-08-11 08:10:42
【问题描述】:
在这段代码中,我试图在 MongoDB 数据库中添加一个新字段。但它在update 变量中给了我一个问题,那就是go.mongodb.org/mongo-driver/bson/primitive.E composite literal uses unkeyed fields。我不知道该怎么办。
错误出在这部分代码上。
{"$set", bson.D{
primitive.E{Key: fieldName, Value: insert},
}},
代码
func Adddata(fieldName, insert string) {
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
collection := client.Database("PMS").Collection("dataStored")
filter := bson.D{primitive.E{Key: "password", Value: Result1.Password}}
update := bson.D{
{"$set", bson.D{
primitive.E{Key: fieldName, Value: insert},
}},
}
_, err = collection.UpdateOne(context.TODO(), filter, update)
if err != nil {
log.Fatal(err)
}
}
【问题讨论】:
标签: mongodb go mongo-go composite-literals