MongoDB学习日记(六):CRUD - update

基本语法
db.user.update( condition , operator )
  • condition 就是查询条件,参考 find( ) 即可
  • operator 就是更新的操作,也可以是一个 document

这里不需要再说 find( ) ,直接讲讲 operator 的内容。另外需要注意的是,如果有多条数据符合要求,MongoDB 只会默认只会修改第一条数据,后面会讲如何全部修改
(介于某个坑货的要求,之后的内容会多贴一些实际操作图片)


整体更新

先给出资源吧(这是之前插入的数据,之后的 update 操作也是以此为依据):
资源

之前也讲过 js 的代码在 MongoDB 的客户端基本都行的通,那么我们也可以定义一个变量来存查询结果,这里我是用 findOne( ) 方法来获取一个 document,之前 find 部分好像忘了讲这个了,不过没关系,这里补充下。find( ) 会得到多个结果,findOne( ) 会默认选取第一个作为返回结果:

var user = db.user.findOne( { name : "admin" } )

findOne 得到 document
(注:当然你还可以定义数组,也可以使用 for 循环等等,这个就不演示了。另外该结果中的lastModified 属性是我之前测试 update 之后的数据,它是一个时间类型 IOSDate,使用的是经度为0度的时间,我们中国现在使用的是 UTC+08:00,也就是在该时间基础上 +8h)

查询得到的 user , 我们来对它进行整体更新,例如修改它的 age = 20

> user.age = 20
20 
> db.user.update( 
>   { name : "admin" },
>   user
> )

操作示例
这里就是将 user 作为一个 document 进行整体更新操作。


局部更新

这部分主要包含两个语法:

  • $inc : increase 的简写,意思就是增加,想想也知道它值针对数值类型
  • $set : 这就和 SQL 中的 set 一个意思,

另外还有一些语法:

  • $currentDate : true / false 。这个就是上面讲到的 lastModified 属性修改的语法,为true 时会修改属性,false不修改

那我来操作一把(将 admin 的年龄设置位30,在设置回20):

db.user.update(
   { "name" : "admin" },
   { $inc : { age : 10 } }
)

inc
(这里就是采用 $inc 对 admin 的年龄+10 , 另外不要问我为什么你有时候写 “name”,有时候直接写 name 啊,MongoDB 都能够识别属性 key 的,不过不会识别值 value,之前的日记也讲过了,喜欢加 “” 了就加,不爽就不加,就是这么任性 )

db.user.update(
   { "name" : "admin" },
   { $set : { age : 20 } }
)

set


upsert

这个词是 MongoDB 的一个特性,该语法的意识是更新时若无此数据则添加

db.user.update( condition , operator , upsert )
  • upsert : true / false:为 true 时,没有则添加,为 false 时,没有不添加
db.user.update(
   { "name" : "admin" },
   { $set : { age : 20 } },
   { upsert : true }
)

db.user.update(
   { "name" : "admin" },
   { $set : { age : 20 } },
   true
)

上面两种语法都可以,第一条是官网给出的规范;不贴图了累死了。


全部更新

一开始也讲到 MongoDB 默认只修改第一条数据,那么怎么修改全部数据呢?

db.user.update( condition , operator , multi)
  • multi : true / false 为 true 时修改全部,为 false 时修改一个
db.user.update(
   { "name" : "admin" },
   { $set : { age : 20 } },
   { multi : true }
)

注意:这里不能像 upsert 一样只写一个 true,不过 multi 和 upsert 一起用时可以

db.user.update(
   { "name" : "admin" },
   { $set : { age : 20 } },
   true,
   true
)

注意:上面的代码示例,我想你也大概明白了,multi 和 upsert 的使用,在没写明时,它是有顺序的,第一个 true 代表的是 upsert ,第二个 true 才是 multi

…… ,作为一个有良心的博主,我还是贴下图吧,待会儿有被某某货喷
这里写图片描述
可以看到设置一个 true 时,后面添加的 admin 的 age 并没有被修改,而设置两个 true 时,admin 的 age都变成25了。


save

语法:

db.user.save( { _id : ObjectId() , NEW_DATA } )

例:

db.user.save( { _id : ObjectId(55f14ac67c7c7177ef34c9d8) , name : "admin_1" } )

执行后你会发现 _id = ObjectId(55f14ac67c7c7177ef34c9d8) 的数据本来是 name = admin ,age = 25,现在只剩 name = admin _1 了,这也就是告诉你:save( ) 会替换掉 _id 为你输入的值的 document 的全部属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值