duanli12176 2016-08-03 15:13
浏览 178

如何在Go中使用mgo对文档进行部分和全部更新

From Web Development with Go by Shiju Varghese, about the update method in the MongoDB driver mgo in Go

Updating Documents

The Update method of the Collection type allows you to update existing documents. Here is the method signature of the Update method:

func (c *Collection) Update(selector interface{}, update interface{}) error

The Update method finds a single document from the collection, matches it with the provided selector document, and modifies it based on the provided update document. A partial update can be done by using the keyword "$set" in the update document.

Listing 8-14 updates an existing document.

err := c.Update(bson.M{"_id": id},
bson.M{"$set": bson.M{
"description": "Create open-source projects",
"tasks": []Task{
Task{" Evaluate Negroni Project", time.Date(2015, time.August, 15, 0, 0, 0, 
0, time.UTC)},
Task{" Explore mgo Project", time.Date(2015, time.August, 10, 0, 0, 0, 0, 
time.UTC)},
Task{" Explore Gorilla Toolkit", time.Date(2015, time.August, 10, 0, 0, 0, 0, 
time.UTC)},
},
}})

A partial update is performed for the fields’ descriptions and tasks. The Update method finds the document with the provided _id value and modifies the fields based on the provided document.

The example is a partial update. It uses two-level nested bson.M to create update interface{}. Does a partial update always use such two-level nested bson.M?

Does a full update use one-level bson.M, such as

err := c.Update(bson.M{"_id": id},
bson.M{ "description": "Create open-source projects",
"tasks": []Task{
Task{" Evaluate Negroni Project", time.Date(2015, time.August, 15, 0, 0, 0, 
0, time.UTC)},
Task{" Explore mgo Project", time.Date(2015, time.August, 10, 0, 0, 0, 0, 
time.UTC)},
Task{" Explore Gorilla Toolkit", time.Date(2015, time.August, 10, 0, 0, 0, 0, 
time.UTC)},
},
})

Thanks.

  • 写回答

1条回答 默认 最新

  • donglugou6652 2016-08-03 22:22
    关注
    1. Yes, the partial update always requires $set. This is not about Go this is about Mongo <kbd>$set operator</kbd> bson.M is just the shortcut for map[string]interface{} which allows us to build JSON-like structures in go without thinking about argument type. So, as you always need $set the update document always will have at least two levels of bson.M. Actually, there are more levels as your Task objects could be defined as bson.M objects too.

    2. Yes, the full document update (personally I think this process looks more like replace) requires only 2 bson.M objects - the query and the new document.

      In Mongo full update looks like

      db.collection.update({_id: "id"}, {name: "name", num: 1})

      And the same command in Go:

      c.Update(bson.M{"_id": "id"}, bson.M{"name": "name", "num": 1})

    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题