对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"
}
]
}