Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-889

How to Collection.UpdateOne using the result of bson.Marshal?

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None

    Description

      For example I have struct like this:

      type Profile struct

      { Vip uint32 Lvl uint32 Heros Heros }

      type Heros struct

      { HeroIds []uint32 `bson:"hids"` HeroSkills []HeroSkill `bson:"hskls"` }

      type HeroSkill struct

      { CounterSkill []string PassiveSkill []string TriggerSkill []string }

       

      than, I want to set a record to collection using UpdateOne.

       

      id := "alsdkfj"
      collection := db.Collection("profiles")

      heros := Heros

      { HeroIds: []uint32\{1, 2, 3}

      ,
      }
      bsb, err := bson.Marshal(heros)
      if err != nil

      { log.Fatal("bson marshal err", err) return }

      update := bsonx.Doc{
      {"$set", bsonx.Document(bsonx.Doc{{"Vip", bsonx.Int32(3)}})},
      {"$set", bsonx.Document(bsonx.Doc{{"Level", bsonx.Int32(10)}})},
      {"$set", bsonx.Document(bsonx.Doc{{"Heros", bsb}})}, // this not work
      }
      res, err := collection.UpdateOne(ctx, bson.D{{"_id", id}}, update, options.Update().SetUpsert(true))

       

      I don't find any way to transform []byte to bsonx.Val, so can put the result of bson.Marshal to bsonx.Doc.

      If I want to update complex construction conveniently,how should I do?

      Please help me, thanks!

      Attachments

        Activity

          People

            Unassigned Unassigned
            zhangzhen zhangzhen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: