【mongoDB 操作】--update, remove, $set, $push

对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
>

========

数组修改器

  1. 增加或者修改:
    –如果数组已经存在,“$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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木瓜~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值