partial update using mgo

前端 未结 2 862
孤独总比滥情好
孤独总比滥情好 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:12

    The solution can be:
    1. client json -> struct -> xml -> map -> database
    2. Update partital with $set operator:

    collection.Update(bson.M{"_id": id}, bson.M{"$set": bson.M{"name": "new Name"}})
    

    Read more: http://docs.mongodb.org/manual/reference/operator/update/set/
    3. Load the document that is going to be updated and store it in a tmp variable. Parse the user input to another variable. Overwrite the values you need to preserve before updating.

提交回复
热议问题