mongodb修改和删除操作

修改数据
修改里面还有查询条件。你要该谁,要告诉 mongo。
查找名字叫做小明的,把年龄更改为 16 岁:
1 db.student.update({"name":"小明"},{$set:{"age":16}});
查找数学成绩是 70,把年龄更改为 33 岁:


1 db.student.update({"score.shuxue":70},{$set:{"age":33}});
更改所有匹配项目:"


By default, the update() method updates a single document. To update multiple documents, use
the multi option in the update() method.


1 db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});
完整替换,不出现$set 关键字了: 注意


1 db.student.update({"name":"小明"},{"name":"大明","age":16});
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
相当于:update users set age = age + 50 where name = ‘Lisi’;
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);
相当于:update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;


删除数据
db.collectionsNames.remove( { "borough": "Manhattan" } )
db.users.remove({age: 132});
By default, the remove() method removes all documents that match the remove condition. Use
the justOne option to limit the remove operation to only one of the matching documents.
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )

转载于:https://www.cnblogs.com/xiaohuangmao/p/10164166.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值