【mongoDB 操作】--update, remove, $set, $push
来源:互联网 发布:网络媒介类型主要有 编辑:程序博客网 时间:2024/06/05 09:22
对mongo进行简单的总结,当然可以阅读mongoDB的reference咯
https://docs.mongodb.com/v2.6/reference/method/db.collection.update/
元素的 增,删,改
1:增加和修改元素, 用update 和$set。更改数据库某个元素的某个字段 用 update和$set,(当然,如果这个字段不存在时,则创建它。)
语法如下:
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> })
db.pr_goal_mapping.update({system_release: "TL16"},{$set: {goal_name:"NCDR"}},false,true) //更改符合条件的所有item。
参数说明:
EXAMPLE:
For example, given a books collection with the following document:
{ _id: 1, item: "TBD", stock: 0, info: { publisher: "1111", pages: 430 }, tags: [ "technology", "computer" ], ratings: [ { by: "ijk", rating: 4 }, { by: "lmn", rating: 5 } ], reorder: false}
- the $inc operator to increment the stock field; and
- the $set operator to replace the value of the item field, the publisher field in the info embedded document, the tags field, and the second element in the ratings array.
db.books.update( { _id: 1 }, { $inc: { stock: 5 }, $set: { item: "ABC123", "info.publisher": "2222", tags: [ "software" ], "ratings.1": { by: "xyz", rating: 3 } } })
The updated document is the following:
{ "_id" : 1, "item" : "ABC123", "stock" : 5, "info" : { "publisher" : "2222", "pages" : 430 }, "tags" : [ "software" ], "ratings" : [ { "by" : "ijk", "rating" : 4 }, { "by" : "xyz", "rating" : 3 } ], "reorder" : false}
例子2:
比如原始数据为:
> db.pci_hierarchy.find({branch:"tl16a_mp", test_hierarchy:/fsih hw cpri dm/}).pretty(){ "_id" : ObjectId("57bac95bcdadc2c83b2d1feb"), "bl" : "lte-n", "product" : "tdd-macro", "branch" : "tl16a_mp", "type" : "product", "test_hierarchy" : "qt;qt1;fsih hw cpri dm", "order" : 999, "createtime" : 1471859035, "updatetime" : 1471859035, "active" : 1}
需要修改 “active” : 1 —> “active” : 0 则用:
> db.pci_hierarchy.update({branch:"tl16a_mp", test_hierarchy:/fsih hw cpri dm/}, {"$set": {"active": 0}})
更改后会有如下提示:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
最后再次检查修改是否生效:
> db.pci_hierarchy.find({branch:"tl16a_mp", test_hierarchy:/fsih hw cpri dm/}).pretty(){ "_id" : ObjectId("57bac95bcdadc2c83b2d1feb"), "bl" : "lte-n", "product" : "tdd-macro", "branch" : "tl16a_mp", "type" : "product", "test_hierarchy" : "qt;qt1;fsih hw cpri dm", "order" : 999, "createtime" : 1471859035, "updatetime" : 1471859035, "active" : 0}
==========
- 2: 删除元素, remove
- remove函数可以接受一个查询文档作为可选参数,给定这个参数后,只有符合条件的文档才被删除。
> db.pci_hierarchy.findOne({"name":"zhou"}){ "_id" : ObjectId("57d0bf74ff127ecc2ac2788d"), "name" : "zhou", "comments" : [ { "name" : "joe", "email" : "joe@example.com" }, { "name" : "chang", "email" : "chang@example.com" } ], "tel" : [ 123, 321 ]}删除该元素:> db.pci_hierarchy.remove({"name":"zhou"})WriteResult({ "nRemoved" : 1 })> db.pci_hierarchy.findOne({"name":"zhou"})null>
========
数组修改器
- 增加或者修改:
–如果数组已经存在,“$push”会向已有的数组末尾添加一个元素,要是不存在就创建一个新的数组。
原始数组:> db.pci_hierarchy.findOne({"name":"zhou"}){ "_id" : ObjectId("57d0bf74ff127ecc2ac2788d"), "name" : "zhou" }利用$push增加一个数组元素:> db.pci_hierarchy.update({"name":"zhou"}, {"$push": {"comments": {"name": "joe", "email": "joe@example.com"}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.pci_hierarchy.findOne({"name":"zhou"}){ "_id" : ObjectId("57d0bf74ff127ecc2ac2788d"), "name" : "zhou", "comments" : [ { "name" : "joe", "email" : "joe@example.com" } ]}再次增加comments的元素,修改成功。> db.pci_hierarchy.update({"name":"zhou"}, {"$push": {"comments": {"name": "chang", "email": "chang@example.com"}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.pci_hierarchy.findOne({"name":"zhou"}){ "_id" : ObjectId("57d0bf74ff127ecc2ac2788d"), "name" : "zhou", "comments" : [ { "name" : "joe", "email" : "joe@example.com" }, { "name" : "chang", "email" : "chang@example.com" } ]}
0 0
- 【mongoDB 操作】--update, remove, $set, $push
- mongodb-基础-update-remove
- MongoDB小结06 - update【$push】
- Mongodb的update操作
- mongodb 数据库update 操作
- Mongodb的update操作
- Mongodb的update操作
- mongoDB update操作
- Mongodb的update操作
- mongodb update操作
- Mongodb的update操作
- Mongodb的update操作
- Mongodb的update操作
- Python 验证MongoDB中$set、$inc、$push几种修改器操作的速度
- MongoDB小结05 - update【$set & $unset】
- mongoDB的update相关操作
- mongodb高级操作(1)-update
- mongodb,update和 findAndModify操作。
- LeetCode136—Single Number
- JavaEE项目实战(OA系统)之二_数据库设计
- Window批处理命令教程
- 我要改变!我要改变!我要改变!
- leetcode Interleaving String
- 【mongoDB 操作】--update, remove, $set, $push
- 使用libcurl提示 LNK2001的错误
- Android Studio 百度地图开发准备工作以及问题分析
- limit分页语句用法
- 怎么共享打印机
- 0xa0是什么意思
- 下载文件并打开
- 第2周 项目0 宣告“主权”
- 几个常见javaee框架问题