MongoDB - 修改文档

一、文档替换

    文档替换其实在之前已经有讲过了,就是传入两个对象,第一个对象作为调节,第二个条件作为满足条件的文档修改的内容,例如:

    db.test.update({"name":"kevin-01"} , {"name": "kevin"});   # 修改  前面条件为,后面为修改的属性和值

 

二、修改器进行修改

    文档提交比较好理解,而修改器是在后面一个文档为对象的基础上,将对象的根key作为修改器类型,后面的对象为真正修改操作的对象,如: {"$set": {} }

1、$set $unset

    db.test.update({"name":"kevin-01", {"$set":{"age":NumberInt("18")}}}); # 满足条件:修改年龄字段,没有年龄字段则进行添加

    db.test.update({"name":"kevin-01"},{"$unset":{"age": 1 }});               # 满足条件:删除age字段

2、$inc(减少值可以用负数)

    db.test.update("name":"kevin-01",{"$inc":{"age": 5}});  # 满足条件,年龄加5岁(负数表示减去

3、$push ($each、$slice、 $sort)

    1)、 往数组对象中添加一个元素

    db.test.update({"name":"kevin-01"},{"$push":{"comments":{"id":"1","content":"content-01"}}});

 

    2)、往数组中添加多个元素,使用$each

    commentsArr = [{"id":NumberInt("2","content":"content-02")},{"id":NumberInt("3","content":"content-03")}];

    db.test.update("name":"kevin-01",{"$push":{"$each": commentsArr }});

    3)、往数组中添加多个元素,并进行排序($sort,值为1表示正序,-1反之),只保留最近的多少个元素($slice,值只能为负数)

    commentsArr = [{"id":NumberInt("2","content":"content-02")},{"id":NumberInt("3","content":"content-03")}];

    db.test.update(user, {"$push":{"comments":
           {"$each":[{"id":NumberInt("2"),"content":"content-02"},{"id":NumberInt("3"),"content":"content-03"}],
            "$slice":-2,
            "$sort":{"id":-1}

    }}});

4、$addToSet

    往数组中添加的数据,为了保证不重复,则可以使用$addToSet,并且可以配置$each进行使用,就想配合$push一样:

    db.test.update({"name":"kevin-01"},{"$addToSet":{"email":"kevin-01@163.com"}});

    db.test.update({"name":"kevin-01"},{"$addToSet":{"email":{"$each":["kevin-01@163.com","kevin-02@163.com"]}}});

5、$pop、$pull

    1)、从数组的尾部或者头部删除一个元素, 1表示重尾部删除,-1表示从头部删除

    db.test.update({"name":"kevin-01"},{"$pop":{"email":1}});

    2)、从数组中删除满足条件的元素

    db.test.update({"name":"kevin-01"},{"$pull":{"email":"kevin-01@163.com"}});

三、upsert(添加或更新文档)

    1)、upinsert

    # 更新的时候,第一个对象为条件,第二个对象为修改的内容,第三个为true,表示新增或修改

    db.test.update({"name":"kevin-01"},{"classNo":NumberInt("5")},true); 

    2)、$setOnInsert

    # 只保证文档在插入时生效,修改时候则保持不变,如创建时间(createdAt)字段,这也是阿里手册规范

    db.test.update({"name":"kevin-01",{"$setOnInsert":{"createdAt":new Date()}}, true});

    3)、save(方便操作,直接见下图)

三、更新多个文档

    update的第一个参数对象为条件,第二个参数对象为更新的对象,第三个为是否upinsert,第四个为是否修改所有满足条件的所有文档,默认关闭,只修改满足条件的第一条

    db.test.update({条件},{修改},false, true);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值