partial update using mgo

前端 未结 2 863
孤独总比滥情好
孤独总比滥情好 2021-02-14 07:27

I have the following problem. I need to convert a structure to map[string]interface{} in order to perform an update in database (using mgo as driver fo

2条回答
  •  独厮守ぢ
    2021-02-14 08:16

    If you are using go.mongodb.org/mongo-driver, you will have to use UpdateOnemethod to update the specific fields. Here is the code

        database := db.Conn.Database("myDatabase")
        coll := database.Collection("myCollection")
        
        filter := bson.M{"_id": bson.M{"$eq": objectHexID}}
        update := bson.M{"$set": bson.M{"useremail": "abc@email.com"}}
    
        updated, err := coll.UpdateOne(ctx, filter, update)
    

提交回复
热议问题