手记

mongo-04-更新和删除

1. 更新文档

1.1 update()

db.<collection>.update(<query>, <update> ,<options>)

  • <query> 文档筛选条件
  • <update> 更新内容
  • <options> 文档声明了一些更新操作的参数

1.1.1 文档替换

如果 <update> 文档不包含任何更新操作符, db.<collection>.update() 将会使用 <update> 文档直接替换集合中符合 <query> 文档筛选条件的文档。

下面将 zhangsan 的 money 改为 2500,结果整个文档都被替换了

> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "money" : 2000, "name" : "zhangsan", "addr" : [ "广州", "北京" ] }
> db.user.update({name: "zhangsan"},{name: "zhangsan",money: 2500})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500 }
> 

注意:

  • 文档主键 _id 是不可以更改的,如果我们在 <update> 文档中包含 _id 字段,则 _id 值一定要和被更新的文档 _id 保持一致。

_id 值一定要和被更新的文档 _id 保持一致

> db.user.update({name: "zhangsan"},{name: "zhangsan",money: 2500,_id: 10})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 0,
	"nModified" : 0,
	"writeError" : {
		"code" : 66,
		"errmsg" : "After applying the update, the (immutable) field '_id' was found to have been altered to _id: 10.0"
	}
})
> db.user.update({name: "zhangsan"},{name: "zhangsan",money: 2500,_id: ObjectId("5fff955d3f6d5ed2fcce4132")})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
  • 在使用 <update> 文档替换整篇被更新文档时,只有第一个符合 <query> 文档筛选条件的文档会被更新

只有第一个符合 <query> 文档筛选条件的文档会被更新

> db.user.find()
{ "_id" : "1", "money" : 1000, "name" : "刘一" }
{ "_id" : "2", "money" : 1000, "name" : "陈二" }
{ "_id" : "3", "money" : 1000, "name" : "张三" }
{ "_id" : "4", "money" : 1000, "name" : "李四" }
{ "_id" : "5", "money" : 1000, "name" : "王五" }
{ "_id" : "6", "money" : 1000, "name" : "赵六" }
{ "_id" : "7", "money" : 1000, "name" : "孙七" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九" }
{ "_id" : ObjectId("5fff8fe79dbe959b53a603b6"), "money" : 3000, "name" : "广东人", "addr" : [ "广州", "深圳" ] }
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500 }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ] }
> db.user.update({money: 1000},{money : 1500, name : "刘一一"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : "1", "money" : 1500, "name" : "刘一一" }
{ "_id" : "2", "money" : 1000, "name" : "陈二" }
{ "_id" : "3", "money" : 1000, "name" : "张三" }
{ "_id" : "4", "money" : 1000, "name" : "李四" }
{ "_id" : "5", "money" : 1000, "name" : "王五" }
{ "_id" : "6", "money" : 1000, "name" : "赵六" }
{ "_id" : "7", "money" : 1000, "name" : "孙七" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九" }
{ "_id" : ObjectId("5fff8fe79dbe959b53a603b6"), "money" : 3000, "name" : "广东人", "addr" : [ "广州", "深圳" ] }
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500 }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ] }
> 

1.2 findAndModify 查找并更新

更新操作符,更新特定字段
如果 <update> 文档只包含更新操作符,则 db.<collection>.update() 将会使用 <update> 文档更新集合中符合 <query> 文档筛选条件的文档中的特定字段

  • $set 更新或新增字段
  • $unset 删除字段
  • $rename 重命名字段
  • $inc 加减字段值
  • $mul 相乘字段值
  • $min 比较减小字段值
  • $max 比较增大字段值

1.2.1 $set 更新或新增字段

给 李四 新增 addr 字段

> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四" }
> db.user.update({name: "李四"},{$set: {addr: ["黑龙江"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四", "addr" : [ "黑龙江" ] }
> 

更新 李四 的 addr 为 哈尔滨

> db.user.update({name: "李四"},{$set: {addr: ["哈尔滨"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四", "addr" : [ "哈尔滨" ] }
> 

1.2.2 $unset 删除字段

删除 李四 的 addr

> db.user.update({name: "李四"},{$unset: {addr: ""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四" }
>

$unset 命令中的赋值(比如上面的 {addr: “”} 和下面的 {addr: “泰国”})对操作结果并没有任何的影响

> db.user.update({name: "李四"},{$set: {addr: ["哈尔滨"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四", "addr" : [ "哈尔滨" ] }
> db.user.update({name: "李四"},{$unset: {addr: "泰国"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四" }
> 

如果 $unset 命令中的字段不存在,那么文档内容将保持不变

> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四" }
> db.user.update({name: "李四"},{$unset: {addr: ""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四" }
> 

1.2.8 $rename 重命名字段

{ $rename: {field1: <newName1>,field2: <newName2>,...}}

如果 $rename 要重命名的字段并不存在,那么文档内容不会被改变

> db.user.find({name: "张三"})
{ "_id" : "3", "money" : 1000, "name" : "张三" }
> db.user.update({name: "张三"},{$rename: {addr: "addrs"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.user.find({name: "张三"})
{ "_id" : "3", "money" : 1000, "name" : "张三" }
> 

如果 $rename 要重命名的字段存在,则重命名字段

> db.user.find({name: "张三"})
{ "_id" : "3", "money" : 1000, "name" : "张三" }
> db.user.update({name: "张三"},{$rename: {money: "sal"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "张三"})
{ "_id" : "3", "name" : "张三", "sal" : 1000 }
> 

当 $rename 命令中的新字段存在的时候, $rename 会先 $unset 新旧字段,然后再 $set 新字段

1.2.4 $inc 加减字段值

{$inc: {field1: <amount1>,...}}

给李四的money加200

> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1000, "name" : "李四" }
> db.user.update({name: "李四"},{$inc: {money: 200}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 1200, "name" : "李四" }
> 

如果被更新的字段不存在,$inc 会创建字段,并且将字段值设为命令中的增减值

> db.user.find({name: "王五"})
{ "_id" : "5", "money" : 1000, "name" : "王五" }
> db.user.update({name: "王五"},{$inc: {sal: 500}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "王五"})
{ "_id" : "5", "money" : 1000, "name" : "王五", "sal" : 500 }
> 

1.2.5 $mul 相乘字段值

{$mul: {field1: <amount1>,...}}

将李四的money缩减一半

> db.user.update({name: "李四"},{$mul: {money: 0.5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 600, "name" : "李四" }
> 

如果被更新的字段不存在,$mul 会创建字段,但是把字段值设为0

> db.user.find({name: "赵六"})
{ "_id" : "6", "money" : 1000, "name" : "赵六" }
> db.user.update({name: "赵六"},{$mul: {sal: 5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "赵六"})
{ "_id" : "6", "money" : 1000, "name" : "赵六", "sal" : 0 }
> 

1.2.6 $min 比较减小字段值,设置为字段值和数值中比较小的那个

{$min: {field1: <value1>,...}}

> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 600, "name" : "李四" }
> db.user.update({name: "李四"},{$min: {money: 500}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 500, "name" : "李四" }
> 

如果被更新的字段不存在,$min 会创建字段,并且将字段值设为命令中的更新值

1.2.7 $max 比较增大字段值,设置为字段值和数值中比较大的那个

{$max: {field1: <value1>,...}}

> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 500, "name" : "李四" }
> db.user.update({name: "李四"},{$max: {money: 800}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "李四"})
{ "_id" : "4", "money" : 800, "name" : "李四" }
> 

如果被更新的字段不存在,$max 会创建字段,并且将字段值设为命令中的更新值

1.2.8 数组更新操作符

  • $addToSet 向数组中增添元素
  • $pop 从数组中移除元素
  • $pull 从数组中要有选择性地移除元素
  • $pullAll 从数组中要有选择性地移除元素
  • $push 向数组中增添元素

1.2.8.1 $addToSet 向数组中增添元素

{$addToSet: {<field1>:<value1>,...}} 向数组字段中,添加元素,如果字段不存在,会自动新增字段

给 zhangsan 新增数组字段 addr,并一次增加 “珠海”,“东莞”

> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500 }
> db.user.update({name: "zhangsan"},{$addToSet: {addr: ["珠海"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ [ "珠海" ] ] }
> db.user.update({name: "zhangsan"},{$addToSet: {addr: ["东莞"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ [ "珠海" ], [ "东莞" ] ] }
> 

如果要插入的值已经存在数组字段中,则 $addToSet 不会再添加重复值

> db.user.update({name: "zhangsan"},{$addToSet: {addr: ["东莞"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ [ "珠海" ], [ "东莞" ] ] }
> 

1.2.8.2 $pop 从数组中移除元素

{$pop: {<field1>:<-1 | 1>,...}} 从数组中删除元素

  • 1 : 删除数组最后一个元素
  • -1 : 删除数组第一个元素
> db.user.update({name: "zhangsan"},{$addToSet: {addr: ["佛山"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ [ "珠海" ], [ "东莞" ], [ "佛山" ] ] }
# 删除数组最后一个元素
> db.user.update({name: "zhangsan"},{$pop: {addr: 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ [ "珠海" ], [ "东莞" ] ] }
# 删除数组第一个元素
> db.user.update({name: "zhangsan"},{$pop: {addr: -1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ [ "东莞" ] ] }
# 删除完数组中的所有元素后,会留下空数组
> db.user.update({name: "zhangsan"},{$pop: {addr: 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "zhangsan"})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ ] }
> 

1.3 update 命令的选项 options

1.3.1 {multi: <boolean>} 更新多个文档

注意:

  • 在默认情况下,即使筛选条件对应了多篇文档,update 命令仍然只会更新第一篇文档。
  • MongoDB 只能保证单个文档操作的原子性,不能保证多个文档操作的原子性,如果需要保证多个文档更新操作的原子性,需要使用事务功能。

现在所有的文档中都没有 gender 这个字段

> db.user.find({})
{ "_id" : "1", "money" : 1500, "name" : "刘一一" }
{ "_id" : "2", "money" : 1000, "name" : "陈二" }
{ "_id" : "3", "name" : "张三", "sal" : 1000 }
{ "_id" : "4", "money" : 800, "name" : "李四" }
{ "_id" : "5", "money" : 1000, "name" : "王五", "sal" : 500 }
{ "_id" : "6", "money" : 1000, "name" : "赵六", "sal" : 0 }
{ "_id" : "7", "money" : 1000, "name" : "孙七" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九" }
{ "_id" : ObjectId("5fff8fe79dbe959b53a603b6"), "money" : 3000, "name" : "广东人", "addr" : [ "广州", "深圳" ] }
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ ] }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ] }
> 

设置 gender 为“女”,但只有第一个文档被更改

> db.user.update({},{$set: {gender: "女"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({})
{ "_id" : "1", "money" : 1500, "name" : "刘一一", "gender" : "女" }
{ "_id" : "2", "money" : 1000, "name" : "陈二" }
{ "_id" : "3", "name" : "张三", "sal" : 1000 }
{ "_id" : "4", "money" : 800, "name" : "李四" }
{ "_id" : "5", "money" : 1000, "name" : "王五", "sal" : 500 }
{ "_id" : "6", "money" : 1000, "name" : "赵六", "sal" : 0 }
{ "_id" : "7", "money" : 1000, "name" : "孙七" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九" }
{ "_id" : ObjectId("5fff8fe79dbe959b53a603b6"), "money" : 3000, "name" : "广东人", "addr" : [ "广州", "深圳" ] }
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ ] }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ] }
> 

加上 multi: true 再次更改,所有匹配到筛选条件的文档都被更改了

> db.user.update({},{$set: {gender: "女"}},{multi: true})
WriteResult({ "nMatched" : 12, "nUpserted" : 0, "nModified" : 11 })
> db.user.find({})
{ "_id" : "1", "money" : 1500, "name" : "刘一一", "gender" : "女" }
{ "_id" : "2", "money" : 1000, "name" : "陈二", "gender" : "女" }
{ "_id" : "3", "name" : "张三", "sal" : 1000, "gender" : "女" }
{ "_id" : "4", "money" : 800, "name" : "李四", "gender" : "女" }
{ "_id" : "5", "money" : 1000, "name" : "王五", "sal" : 500, "gender" : "女" }
{ "_id" : "6", "money" : 1000, "name" : "赵六", "sal" : 0, "gender" : "女" }
{ "_id" : "7", "money" : 1000, "name" : "孙七", "gender" : "女" }
{ "_id" : ObjectId("5fff6a78025e5d9ea86e18cc"), "money" : 1000, "name" : "周八", "gender" : "女" }
{ "_id" : { "age" : 18, "gender" : "男" }, "money" : 1000, "name" : "吴九", "gender" : "女" }
{ "_id" : ObjectId("5fff8fe79dbe959b53a603b6"), "money" : 3000, "name" : "广东人", "addr" : [ "广州", "深圳" ], "gender" : "女" }
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ ], "gender" : "女" }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ], "gender" : "女" }
> 

1.3.2 {upsert: <boolean>} 更新或创建文档

{upsert: <boolean>}
在默认情况下,upsert=false,如果update命令中的筛选条件没有匹配任何文档,则不会进行任何的操作。
将 upsert 选项设置为true,如果update命令中的筛选条件没有匹配任何文档,则会创建新文档。

# name为“富二代”的文档不存在
> db.user.find({name: "富二代"},{name: 1, money: 1, _id: 0})
# 默认情况下,upsert=false,没有匹配任何文档,则不会进行任何的操作
> db.user.update({name: "富二代"},{$inc: {money: 1500}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.user.find({name: "富二代"},{name: 1, money: 1, _id: 0})
# upsert=false,没有匹配任何文档,则不会进行任何的操作
> db.user.update({name: "富二代"},{$inc: {money: 1500}},{upsert: false})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.user.find({name: "富二代"},{name: 1, money: 1, _id: 0})
>

将 upsert 选项设置为true,如果update命令中的筛选条件没有匹配任何文档,则会创建新文档

> db.user.update({name: "富二代"},{$inc: {money: 1500}},{upsert: true})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("60045f5719414c7b5be4c122")
})
> db.user.find({name: "富二代"},{name: 1, money: 1, _id: 0})
{ "name" : "富二代", "money" : 1500 }
>

不过,如果无法从筛选条件中推断出确定的字段值,新创建的文档将不会包含筛选条件涉及的字段

> db.user.find({name: "打工人"},{name: 1,money: 1, _id: 0})
> db.user.update({money: {$lt: 300}},{$set: {name: "打工人"}},{upsert: true})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("6004611c19414c7b5be4c13f")
})
> db.user.find({name: "打工人"},{name: 1,money: 1, _id: 0})
{ "name" : "打工人" }
> 

1.3 save() 新增或者更新文档

db.<collection>.save(<document>)

如果 document 中不包含 _id 字段,则新增 document 文档。
如果 document 中包含了 _id 字段,save() 命令将会调用 db.<collection>.update(),命令 {upsert: true}

document 中包含了 _id 字段,更新文档

> db.user.find({name: "打工人"})
{ "_id" : ObjectId("6004611c19414c7b5be4c13f"), "name" : "打工人" }
> db.user.save({ "_id" : ObjectId("6004611c19414c7b5be4c13f"), "name" : "打工人", money: 800 })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({name: "打工人"})
{ "_id" : ObjectId("6004611c19414c7b5be4c13f"), "name" : "打工人", "money" : 800 }
> 

2. remove 删除文档

db.<collection>.remove(<query>,<options>)
<options> 文档声明了一些删除操作的参数

删除 name=“广东人” 的文档

# 当前存在 name="广东人" 的文档
> db.user.find({name: "广东人"})
{ "_id" : ObjectId("5fff8fe79dbe959b53a603b6"), "money" : 3000, "name" : "广东人", "addr" : [ "广州", "深圳" ], "gender" : "女" }
# 删除 name="广东人" 的文档
> db.user.remove({name: "广东人"})
WriteResult({ "nRemoved" : 1 })
# 删除成功
> db.user.find({name: "广东人"})
> 

默认情况下,remove 命令会删除所有符合筛选条件的文档,如果只想删除第一篇文档,可以使用 justOne选项

# 现在存在 addr 字段的文档,有两个
> db.user.find({addr: {$exists: true}})
{ "_id" : ObjectId("5fff955d3f6d5ed2fcce4132"), "name" : "zhangsan", "money" : 2500, "addr" : [ ], "gender" : "女" }
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ], "gender" : "女" }
# 只删除 第一篇 存在 addr 字段的文档
> db.user.remove({addr: {$exists: true}},{justOne: true})
WriteResult({ "nRemoved" : 1 })
# 查看删除结果,只删除了一个文档,还剩一个文档
> db.user.find({addr: {$exists: true}})
{ "_id" : ObjectId("5fff95783f6d5ed2fcce4133"), "money" : 2000, "name" : "ZHANG", "addr" : [ "三亚", "北京" ], "gender" : "女" }

3. drop 删除集合

db.<collection>.drop({writeConcern: <document>})
这里的 writeConcern 文档定义了本次集合删除操作的安全写级别。

remove 命令只会删除文档,但不会删除集合,drop 命令可以删除整个集合,包括集合中的所有文档和索引。

删除 user 集合

> show collections
user
> db.user.drop()
true
> show collections
>
0人推荐
随时随地看视频
慕课网APP