partial update using mgo
来源:Golang技术栈
时间:2023-03-31 10:55:25 223浏览 收藏
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《partial update using mgo》,这篇文章主要会讲到golang等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
问题内容
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](https://stackoverflow.com/questions/23589564/function-for-converting-a- struct-to-map-in-golang)
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.
正确答案
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.
以上就是《partial update using mgo》的详细内容,更多关于golang的资料请关注golang学习网公众号!
-
439 收藏
-
262 收藏
-
193 收藏
-
188 收藏
-
500 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习