MongoDB 文档操作(增、删、改)
在MongoDB中文档是指多个键及其关联的值有序地放置在一起就是文档,其实指的就是数据,也是我们平时操作最多的部分
MongoDB中的文档的数据结构和 JSON 基本一样。所有存储在集合中的数据都是 BSON 格式
BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称.
1. 插入单个文档
1.1 insert函数
> db.dev.insert({name:'z3',age:30})
WriteResult({ "nInserted" : 1 })
1.2 save函数
> db.dev.save({name:'l4',age:20})
WriteResult({ "nInserted" : 1 })
1.3 insertOne函数
在MongoDB3.2以后的版本中,提供了insertOne()函数用于插入文档
> db.dev.insertOne({name:'w5',age:25})
{
"acknowledged" : true,
"insertedId" : ObjectId("5f72dd2c8b17a978aa695f6a")
}
2. 批量新增文档
2.1 insert函数或者 save函数
> db.dev.insert([{title:'java',tags:['javase','javaee','javame']},{title:'orm',tags:['mybatis','hibernate']}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
2.3 insertMany 函数
> db.dev.insertMany([{title:'web',tags:['jsp','servlet']},{title:'rpc',tags:['rmi','dubbo']},{title:'database',tags:['oracle','mysql']}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f72df1e8b17a978aa695f6d"),
ObjectId("5f72df1e8b17a978aa695f6e"),
ObjectId("5f72df1e8b17a978aa695f6f")
]
}
3.通过变量插入文档
Mongo Shell工具允许我们定义变量。所有的变量类型为var类型。也可忽略变量类型。变量中赋值符号后侧需要使用小括号来表示变量中的值。我们可以将变量作为任意插入文档的函数的参数
> document=([{title:'SpringCloud',tags:['Spring Cloud Netflix','Spring Cloud Security','Spring Cloud Consul']},{title:'SpringBoot',tags:['Spring Boot']}])
[
{
"title" : "SpringCloud",
"tags" : [
"Spring Cloud Netflix",
"Spring Cloud Security",
"Spring Cloud Consul"
]
},
{
"title" : "SpringBoot",
"tags" : [
"Spring Boot"
]
}
]
> db.dev.insertMany(document);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f72e15e8b17a978aa695f70"),
ObjectId("5f72e15e8b17a978aa695f71")
]
}
db.dev.insert(document);
db.dev.save(document);
db.dev.insertMany(document);
4. 更新文档
MongoDB通过update函数与save函数来更新集合中的文档。
4.1 save
save 函数的作用是保存文档,如果文档存在则覆盖,如果文档不存在则新增。save()函数对文档是否存在的唯一判断标准是"_id"系统唯一字段是否匹配。所以使用save()函数实现更新操作,则必须提供"_id"字段数据。
> db.dev.save({ "_id" : ObjectId("5f72a5368b17a978aa695f67"),title:"k111",tags:["k","111" ]} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
4.2 update
update() 函数用于更新已存在的文档。
语法格式:db.COLLECTION_NAME.update({查询条件},{更新内容},{更新参数(可选)})
> db.dev.update({title:"Spring Data"},{title:"SpringData"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
此示例会将此数据严格按照更新的数据进行覆盖,其他字段将被删除,
4.3. 更新操作符
使用更新操作符可以定向更新指定内容
4.3.1 $set 操作符
用法:{$set:{field:value}}
作用:把文档中某个字段field的值设为value,如果field不存在,则增加新的字段并赋值为value。
> db.dev.update({title:"SpringData"},{$set:{tags:["SpringDataRedis","SpringDataMongoDB"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({title:"SpringData"})
{ "_id" : ObjectId("5f72ed008b17a978aa695f72"), "title" : "SpringData", "tags" : [ "SpringDataRedis", "SpringDataMongoDB" ] }
4.3.2 设置批量更新
update 第三个参数 {multi:true}
> db.dev.insertMany([{title:'dev',size:100},{title:'dev',size:200}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f72f0708b17a978aa695f73"),
ObjectId("5f72f0708b17a978aa695f74")
]
}
> db.dev.update({title:'dev'},{$set:{size:500}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.dev.find({title:'dev'})
{ "_id" : ObjectId("5f72f0708b17a978aa695f73"), "title" : "dev", "size" : 500 }
{ "_id" : ObjectId("5f72f0708b17a978aa695f74"), "title" : "dev", "size" : 500 }
>
4.3.3 $inc 操作符
用法:{$inc:{field:value}}
作用:对一个数字字段的某个field 进行增减操作
> db.dev.update({title:'dev'},{$inc:{size:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({title:'dev'})
{ "_id" : ObjectId("5f72f0708b17a978aa695f73"), "title" : "dev", "size" : 501 }
{ "_id" : ObjectId("5f72f0708b17a978aa695f74"), "title" : "dev", "size" : 500 }
4.3.4 $unset
用法:{$unset:{field:1}}, 1 为占位 ,任何值都行
作用:删除某个字段field
> db.dev.update({title:"SpringData"},{$unset:{tags:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({title:'SpringData'})
{ "_id" : ObjectId("5f72ed008b17a978aa695f72"), "title" : "SpringData" }
>
4.3.5 $pop
用法:删除数组内第一个值:{$pop:{field:-1}}、删除数组内最后一个值:{$pop:{field:1}}
作用:用于删除数组内的一个值
> db.dev.find({"title":"SpringCloud"})
{ "_id" : ObjectId("5f72e15e8b17a978aa695f70"), "title" : "SpringCloud", "tags" : [ "Spring Cloud Netflix", "Spring Cloud Security", "Spring Cloud Consul" ] }
> db.dev.update({"title":"SpringCloud"},{$pop:{tags:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({"title":"SpringCloud"})
{ "_id" : ObjectId("5f72e15e8b17a978aa695f70"), "title" : "SpringCloud", "tags" : [ "Spring Cloud Netflix", "Spring Cloud Security" ] }
>
4.3.6. $push
用法:{$push:{field:value}}
作用:把value追加到field里。注:field只能是数组类型,如果field不存在,会自动插入一个数组类型
> db.dev.find({"title":"SpringCloud"})
{ "_id" : ObjectId("5f72e15e8b17a978aa695f70"), "title" : "SpringCloud", "tags" : [ "Spring Cloud Netflix", "Spring Cloud Security" ] }
> db.dev.update({"title":"SpringCloud"},{$push:{"tags":"Spring Cloud Consul"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({"title":"SpringCloud"})
{ "_id" : ObjectId("5f72e15e8b17a978aa695f70"), "title" : "SpringCloud", "tags" : [ "Spring Cloud Netflix", "Spring Cloud Security", "Spring Cloud Consul" ] }
>
4.3.7 $pull
用法:{$pull:{field:_value}}
作用:从数组field内删除所有等于_value的值
> db.dev.update({"title":"SpringCloud"},{$pull:{"tags":"Spring Cloud Consul"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({"title":"SpringCloud"})
{ "_id" : ObjectId("5f72e15e8b17a978aa695f70"), "title" : "SpringCloud", "tags" : [ "Spring Cloud Netflix", "Spring Cloud Security" ] }
>
4.3.8 $pullAll
用法:{$pullAll:value_array}
作用:用法同$pull一样,可以一次性删除数组内的多个值。
> db.dev.find({title:'java'})
{ "_id" : ObjectId("5f72de3a8b17a978aa695f6b"), "title" : "java", "tags" : [ "javase", "javaee", "javame" ] }
> db.dev.update({title:'java'},{$pullAll:{"tags":["javase","javaee"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({title:'java'})
{ "_id" : ObjectId("5f72de3a8b17a978aa695f6b"), "title" : "java", "tags" : [ "javame" ] }
>
4.3.9 $rename
作用:对字段进行重命名。底层实现是先删除old_field字段,再创建new_field字段。
> db.dev.find({"k1":'v1'})
{ "_id" : ObjectId("5f72a5368b17a978aa695f67"), "k1" : "v1" }
> db.dev.update({"k1":'v1'},{$rename:{"k1":'k11'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dev.find({"k11":"v1"})
{ "_id" : ObjectId("5f72a5368b17a978aa695f67"), "k11" : "v1" }
5. 删除文档
5.1 remove() 函数
语法格式是:
db.集合名称.remove(
);
参数说明:
query:要删除的文档条件,相当于SQL语法中的where子句作用。
justOne:可选参数,布尔类型,代表是否只删除第一个匹配条件满足的文档。默认值为false,代表删除全部满足匹配条件的文档。
只删除一条数据,在remove函数中给定参数1即可。1 代表true 是占位符
注意:此方法已经过时,官方推荐使用deleteOne()和deleteMany()函数来实现删除操作。且在4.0-版本中,remove()函数并不会真正的释放存储空间,需要使用db.repairDatabase()函数来释放存储空间。在4.2.1版本中,删除函数repairDatabase() 使用 compact 。
5.1.1 删除集合中的全部数据
db.dev.remove({})
db.dev.deleteMany({})
5.1.2 条件删除
> db.dev.find({title:'k111'})
{ "_id" : ObjectId("5f72a5368b17a978aa695f67"), "title" : "k111", "tags" : [ "k", "111" ] }
> db.dev.remove({title:'k111'})
WriteResult({ "nRemoved" : 1 })
> db.dev.find({title:'k111'})
5.1.3 只删除一条
> db.dev.find({title:'dev'})
{ "_id" : ObjectId("5f72f0708b17a978aa695f73"), "title" : "dev", "size" : 501 }
{ "_id" : ObjectId("5f72f0708b17a978aa695f74"), "title" : "dev", "size" : 500 }
{ "_id" : ObjectId("5f7c6ab067256af2c63e2b12"), "title" : "dev", "desc" : "test1" }
{ "_id" : ObjectId("5f7c6ab467256af2c63e2b13"), "title" : "dev", "desc" : "test2" }
{ "_id" : ObjectId("5f7c6aba67256af2c63e2b14"), "title" : "dev", "desc" : "test3" }
> db.dev.remove({title:'dev'},1)
WriteResult({ "nRemoved" : 1 })
> db.dev.find({title:'dev'})
{ "_id" : ObjectId("5f72f0708b17a978aa695f74"), "title" : "dev", "size" : 500 }
{ "_id" : ObjectId("5f7c6ab067256af2c63e2b12"), "title" : "dev", "desc" : "test1" }
{ "_id" : ObjectId("5f7c6ab467256af2c63e2b13"), "title" : "dev", "desc" : "test2" }
{ "_id" : ObjectId("5f7c6aba67256af2c63e2b14"), "title" : "dev", "desc" : "test3" }
> db.dev.remove({title:'dev'},true)
WriteResult({ "nRemoved" : 1 })
> db.dev.find({title:'dev'})
{ "_id" : ObjectId("5f7c6ab067256af2c63e2b12"), "title" : "dev", "desc" : "test1" }
{ "_id" : ObjectId("5f7c6ab467256af2c63e2b13"), "title" : "dev", "desc" : "test2" }
{ "_id" : ObjectId("5f7c6aba67256af2c63e2b14"), "title" : "dev", "desc" : "test3" }
5.1.4 db.repairDatabase() compact
4.2版本以后使用 compact命令
> use db1
switched to db db1
> db.runCommand({compact:"db1",force:true})
{ "ok" : 1 }
>
参考 https://docs.mongodb.com/v4.2/release-notes/4.2-compatibility/index.html
https://mongoing.com/archives/26907
5.1.5 deleteOne() 函数
只删除一条数据,删除的同时释放磁盘空间
语法格式:
db.集合名称.deleteOne({
参数解释:
query:要删除的文档条件,相当于SQL语法中的where子句作用。
> db.dev.find({"title":"dev"})
{ "_id" : ObjectId("5f7c6ab067256af2c63e2b12"), "title" : "dev", "desc" : "test1" }
{ "_id" : ObjectId("5f7c6ab467256af2c63e2b13"), "title" : "dev", "desc" : "test2" }
{ "_id" : ObjectId("5f7c6aba67256af2c63e2b14"), "title" : "dev", "desc" : "test3" }
> db.dev.deleteOne({"title":"dev"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.dev.find({"title":"dev"})
{ "_id" : ObjectId("5f7c6ab467256af2c63e2b13"), "title" : "dev", "desc" : "test2" }
{ "_id" : ObjectId("5f7c6aba67256af2c63e2b14"), "title" : "dev", "desc" : "test3" }
5.1.6. deleteMany
语法格式:
db.集合名称.deleteMany({
参数解释:
query:要删除的文档条件,相当于SQL语法中的where子句作用。
> db.dev.find({"title":"dev"})
{ "_id" : ObjectId("5f7c6ab467256af2c63e2b13"), "title" : "dev", "desc" : "test2" }
{ "_id" : ObjectId("5f7c6aba67256af2c63e2b14"), "title" : "dev", "desc" : "test3" }
> db.dev.deleteMany({"title":"dev"})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.dev.find({"title":"dev"})
>