【MongoDB】删除操作

1.删除满足条件的记录 

> db.users.remove({"Gender":"M"})
WriteResult({ "nRemoved" : 1 })
> db.users.find({"Gender":"M"})

以删除男生为例,删除后没用男生的记录。

--删除45岁的女生。
> db.users.find()
{ "_id" : ObjectId("666a5b45adc823c6544594dc"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594dd"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594de"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594df"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e0"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e1"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e2"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e3"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e4"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e5"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e6"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e7"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e8"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e9"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ea"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594eb"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ec"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ed"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ee"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ef"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "UK" }
Type "it" for more
> db.users.remove({"FName" : "Test User", "Age" : 45, "Gender" : "F"})
WriteResult({ "nRemoved" : 1 })
> db.users.find()
{ "_id" : ObjectId("666a5b86adc823c6544594dd"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594de"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594df"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e0"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e1"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e2"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e3"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e4"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e5"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e6"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e7"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e8"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e9"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ea"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594eb"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ec"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ed"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ee"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ef"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594f0"), "Name" : "Test User20", "Age" : 30, "Gender" : "F", "Country" : "UK" }
Type "it" for more

2.执行truncate操作,删除所有的数据。

> db.users.remove();
2024-06-13T10:52:55.987+0800 E QUERY    [js] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:356:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:383:18
@(shell):1:1
在Mongodb4.0.2的版本中,不允许不带任何参数执行删除。必须携带条件删除。

3.drop 集合 

> db.users.drop()
true
> show collections;
drop命令可以删除整个集合。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值