如何解决 “primitive.E 组合字面量使用未命名字段” 错误?

huangapple go评论125阅读模式
英文:

How to remove the primitive.E composite literal uses unkeyed fields error?

问题

在这段代码中,我正在尝试在MongoDB数据库中添加一个新字段。但是在update变量中出现了一个问题,即go.mongodb.org/mongo-driver/bson/primitive.E composite literal uses unkeyed fields。我不知道该怎么办。

错误出现在代码的这部分。

  1. {"$set", bson.D{
  2. primitive.E{Key: fieldName, Value: insert},
  3. }},

代码

  1. func Adddata(fieldName, insert string) {
  2. // 设置客户端选项
  3. clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
  4. // 连接到MongoDB
  5. client, err := mongo.Connect(context.TODO(), clientOptions)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. collection := client.Database("PMS").Collection("dataStored")
  10. filter := bson.D{primitive.E{Key: "password", Value: Result1.Password}}
  11. update := bson.D{
  12. {"$set", bson.D{
  13. primitive.E{Key: fieldName, Value: insert},
  14. }},
  15. }
  16. _, err = collection.UpdateOne(context.TODO(), filter, update)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. }
英文:

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.

  1. {"$set", bson.D{
  2. primitive.E{Key: fieldName, Value: insert},
  3. }},

Code

  1. func Adddata(fieldName, insert string) {
  2. // Set client options
  3. clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
  4. // Connect to MongoDB
  5. client, err := mongo.Connect(context.TODO(), clientOptions)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. collection := client.Database("PMS").Collection("dataStored")
  10. filter := bson.D{primitive.E{Key: "password", Value: Result1.Password}}
  11. update := bson.D{
  12. {"$set", bson.D{
  13. primitive.E{Key: fieldName, Value: insert},
  14. }},
  15. }
  16. _, err = collection.UpdateOne(context.TODO(), filter, update)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. }

答案1

得分: 3

你看到的是一个lint警告,而不是编译器错误。bson.D是一个primitive.E的切片,当列出切片中的primitive.E值时,你使用了一个无键的字面量:

  1. update := bson.D{
  2. {"$set", bson.D{
  3. primitive.E{Key: fieldName, Value: insert},
  4. }},
  5. }

为了消除警告,请在结构字面量中提供键:

  1. update := bson.D{
  2. {Key: "$set", Value: bson.D{
  3. primitive.E{Key: fieldName, Value: insert},
  4. }},
  5. }

请注意,你还可以使用bson.M值来提供更新文档,这样更简单和可读:

  1. update := bson.M{
  2. "$set": bson.M{
  3. fieldName: insert,
  4. },
  5. }
英文:

What you see is a lint warning, but not a compiler error. bson.D is a slice of primitive.E, and you use an unkeyed literal when listing the primitive.E values of the slice:

  1. update := bson.D{
  2. {"$set", bson.D{
  3. primitive.E{Key: fieldName, Value: insert},
  4. }},
  5. }

To get rid of the warning, provide the keys in the struct literal:

  1. update := bson.D{
  2. {Key: "$set", Value: bson.D{
  3. primitive.E{Key: fieldName, Value: insert},
  4. }},
  5. }

Note that alternatively you may use a bson.M value to provide the update document, it's simpler and more readable:

  1. update := bson.M{
  2. "$set": bson.M{
  3. fieldName: insert,
  4. },
  5. }

huangapple
  • 本文由 发表于 2021年5月22日 17:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/67648046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定