In this code, I am trying to add a new field in the MongoDB database. But it is giving me a problem in the update
variable and that is go.mongodb.org/mongo-driver/bson/primitive.E composite literal uses unkeyed fields
. I don't know what to do.
The error is on this part of the code.
{"$set", bson.D{
primitive.E{Key: fieldName, Value: insert},
}},
Code
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)
}
}