使用mgo进行部分更新

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

partial update using mgo

问题

我有以下问题。我需要将一个structure转换为map[string]interface{},以便在数据库中执行更新操作(使用mgo作为mongodb的驱动程序)。

更新

对于部分更新MongoDB中的文档,(最佳)解决方案是将其转换为map并删除不需要的字段。有关从结构体转换为map的方法,请参考我的其他帖子


原始帖子

我从客户端JavaScript接收数据并将其写入我的结构模型。但是,我不想更改/更新某些字段,因此我需要将我的结构体转换为map[string]interface{}以删除不需要的字段。

将结构体转换为JSON,然后再转换为map是不可行的,因为字段类型不会保留。例如,假设以下结构体是一个Image model

type Image struct {
    Name string `json:name`
    Views int `json:views,string`
    Owner string `json:owner`
}

到目前为止还好,但是当我从客户端(即JavaScript)接收到信息时,views字段是一个字符串。如果我将来自客户端的JSON输入转换为map,那么views字段仍然是一个字符串,并且该值的内部表示在数据库中发生了变化。因此,下次我从数据库中读取此图像时,Views字段将被清零(因为它在数据库中的字符串表示)。

因此,我将客户端的JSON输入写入结构体(以正确转换Views变量)。但是,owner值不应更改(来自数据库的值)。因此,在对数据库进行更新之前,我需要再次将结构体转换为map[string]interface{}并处理该map。

对于此操作,使用json包不是一个选项,因为Views字段的字符串标签将会将其从int转换为字符串(在转换为map时)。

到目前为止,我尝试了以下用于将结构体转换为map的函数,并且我使用了反射包,但对于使用它还不熟悉。我对该包的使用还不太了解。

如果您能提出一些想法,我将不胜感激。
谢谢。

英文:

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 for mongodb).

Update

For partially updating a document in mongoDB, the (optimal) solution is to convert to a map and delete the unwanted fields. For converting from struct to map please refer to my other post
<hr/>

Original Post

I receive the data from client side javascript and write in the my struct model. But I don't want to change/update some fields and thus I need to convert my structure to a map[string]interface{} to delete the unwanted fields.

Converting the structure to json and then to map it's not ok because the field types are not preserved. For example, let the followin structure be an Image model:

type Image struct {
    Name string `json:name`
    Views int `json:views,string`
    Owner string `json:owner`
}

So far so good, but when I receive informations from client(i.e. javascript) the views field is a string. If I convert to a map the json input given from client, then the views field remains a string and the internal representation of this value is changed in database. So next time I read this image from database, the Views field is zeroed (because of it's string representation from database).

Thus I write the json input from client in structure (for proper conversion of Views variable). But the owner value shouldn't change (the one form database). So I need to convert again the structure to a map[string]interface{} and process that map before making the update in database.

Using the json package for this it's not an option, because the string tag from Views field will convert from int to string (when converting to map).

So far I've tried the following function for converting structure to map, and I use reflection package and am a noob with using it. Don't quite understand well the package.

I would be grateful if you would come up with some ideas.
Thanks.

答案1

得分: 27

解决方案可以是:

  1. client json -> struct -> xml -> map -> database

  2. 使用$set操作符进行部分更新:

    collection.Update(bson.M{"_id": id}, bson.M{"$set": bson.M{"name": "新名称"}})

了解更多信息:http://docs.mongodb.org/manual/reference/operator/update/set/
3. 加载将要更新的文档并将其存储在一个临时变量中。将用户输入解析到另一个变量中。在更新之前覆盖需要保留的值。

英文:

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.

答案2

得分: 0

如果你正在使用go.mongodb.org/mongo-driver,你需要使用UpdateOne方法来更新特定的字段。以下是代码示例:

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)
英文:

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(&quot;myDatabase&quot;)
	coll := database.Collection(&quot;myCollection&quot;)
	
	filter := bson.M{&quot;_id&quot;: bson.M{&quot;$eq&quot;: objectHexID}}
	update := bson.M{&quot;$set&quot;: bson.M{&quot;useremail&quot;: &quot;abc@email.com&quot;}}

	updated, err := coll.UpdateOne(ctx, filter, update)

huangapple
  • 本文由 发表于 2014年5月10日 23:55:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/23583198.html
匿名

发表评论

匿名网友

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

确定