使用mgo进行部分更新 [英] partial update using mgo

查看:110
本文介绍了使用mgo进行部分更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

要部分更新mongoDB中的文档,(最佳)解决方案是转换为地图并删除不需要的字段.有关从struct转换为map的信息,请参阅我的其他文章

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

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

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.

将结构转换为json,然后进行映射是不可行的,因为未保留字段类型.例如,让followin结构为图片模型:

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`
}

到目前为止很好,但是当我从客户端(即javascript)接收信息时,views字段是一个字符串.如果我将客户端提供的json输入转换为地图,则views字段仍为字符串,并且该值的内部表示在数据库中已更改.因此,下次我从数据库中读取此图像时,视图"字段将被清零(因为它是数据库中的字符串表示形式).

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

因此,我从结构中编写了来自客户端的json输入(用于Views变量的正确转换).但是所有者值不应更改(一种形式的数据库).因此,在数据库中进行更新之前,我需要再次将结构转换为map [string] interface {}并处理该映射.

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.

对此不使用json包,因为视图"字段中的字符串标记将从int转换为字符串(转换为map时).

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. client json -> struct -> xml -> map -> 数据库
2.使用 $ set 运算符更新分词:

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"}})

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

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.

这篇关于使用mgo进行部分更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆