智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述Mongodb 数据库的简单使用相关的知识,希望能为你提供帮助。
一、数据库相关操作
1、创建数据库use 数据库名#切换到指定的数据库,当数据库不存在时,就表示创建这个数据库>
use admin
switched to db admin
>
db.auth("admin","123456")#用户账号和口令
1>
show dbs
admin0.000GB
config0.000GB
local0.000GB
test0.000GB>
use ceshi#创建 数据库
switched to db ceshi>
db#显示现在使用的是哪个数据库
ceshi>
show dbs#数据库里面没有数据时,数据库查询时不显示
admin0.000GB
config0.000GB
local0.000GB
test0.000GB我们向 ceshi 这个数据库里面增加一个集合,再看看查看的情形
>
db.createCollection("class")
"ok" : 1 >
show dbs
admin0.000GB
ceshi0.000GB
config0.000GB
local0.000GB
test0.000GB#此时数据库查询时,ceshi 这个数据库才会显示2、显示数据库show dbs 或者 show databases
>
show dbs
admin0.000GB
config0.000GB
local0.000GB
test0.000GB>
show databases
admin0.000GB
config0.000GB
local0.000GB
test0.000GB3、 数据库信息查看>
use test
switched to db test>
db.stats()"db" : "test",
"collections" : 1,
"views" : 0,
"objects" : 1,
"avgObjSize" : 41,
"dataSize" : 41,
"storageSize" : 20480,
"numExtents" : 0,
"indexes" : 1,
"indexSize" : 20480,
"scaleFactor" : 1,
"fsUsedSize" : 2687488000,
"fsTotalSize" : 53135798272,
"ok" : 14、删除数据库# 删除数据库必须先切换到数据库下,然后使用数据库的内置函数进行删除>
use ceshi
switched to db ceshi>
db
ceshi>
db.dropDatabase()
"dropped" : "ceshi", "ok" : 1 >
show dbs
admin0.000GB
config0.000GB
local0.000GB
test0.000GB# 温馨提示:
1、不同角色看见的数据库数量是不一样的,普通用户只能看见自己的数据库
2、刚创建的数据库里面是没有数据的,此时查询时是无法看到的此数据库的
二、集合相关的操作
# Mongod 中的集合是一组文档的集,集合相当于关系型数据库中的表1、创建集合
创建集合两种方式: 显式创建 和 隐式创建>
db.createCollection("games")#显式创建
"ok" : 1 >
db.info.insert("name":"tom")#隐式创建
WriteResult( "nInserted" : 1 )>
show collections#查看
class
games
info2、查看集合
show collections 或者show tables>
show collections
class
games
info>
show tables
class
games
info3、删除集合
使用数据库的内置函数进行删除>
db.games.drop()
true>
show collections
class
info4、集合改名>
db.info.renameCollection("test")
"ok" : 1
>
>
show collections
class
test5、迁移集合#另外一种叫法:跨数据库重命名集合(当然也可以不换集合的名字)
#注意:必须要有相关权限才能够操作,最直接的是切换到 admin 库下进行操作>
show dbs
admin0.000GB
config0.000GB
local0.000GB
test0.000GB
ceshi0.000GB>
use test
switched to db test>
show collections
class
games
test>
use admin
switched to db admin#迁移并更改集合名字
>
db.runCommand(renameCollection:"test.games",to:"ceshi.gamestest",dropTarget:true)
"ok" : 1 #迁移但是不更改集合名字
>
db.runCommand(renameCollection:"test.test",to:"ceshi.test",dropTarget:true)
"ok" : 1 >
use ceshi
switched to db ceshi>
show collections
gamestest
info
test>
use test
switched to db test>
show collections
class#说明:
显示添加集合完整的语法格式为:db.createCollection( name ,options )name: 是指定还要创建的集合的名字
options: 是一个可选项,具体有如下的选项 ( 一般情况下很少使用这个选项 )
capped:(可选项),为 true 则创建的是固定集合(即制定大小的集合)
如果为 true 时,则必须指定下面 size 选项的大小
当固定集合使用当到达最大值时,后面的文档自动覆盖签名的文档
size:(可选项),限制集合空间的大小,默认没有限制。如果 capped 为 true ,则必须指定此字段
autoindexid: (可选项),如果为 true ,自动中 _id 字段创建索引,默认是 true
max:(可选项),限制集合包含文档的最大数量,默认是没有限制
三、文档相关的操作
1、文档的创建#文档的创建两个内置函数来实现的
db.Collectionname.insert(Documentname) 、db.Collectionname.save(Documentname)1)insert 方法
db.Collectionname.insert()#可插入单条或者多条数据(批量插入)
db.Collectionname.insertOne()#只能插入单条数据
db.Collectionname.insertMany()#只能插入多条数据insert函数的语法格式:
db.collection.insert(Collectionname, writeConcern, ordered)Collectionname: 表示可设置插入一条或多条文档
writeConcern:表示自定义写出错的级别,是一种出错捕捉机制
ordered:<
boolean>
是可选的,默认为 true 。
默认时在执行文档的有序插入,如果其中一个文档发生错误,MongoDB将返回而不处理数组中的其余文档
为 false,则执行无序插入,若其中一个文档发生错误,则忽略错误,继续处理数组中的其余文档# 一般情况下都很少在插入时带上后面两个参数>
db.info.find()
"_id" : 90, "name" : "blue", "age" : 30 >
db.info.insert(name:"itit", age:28)#insert 插入一条数据
WriteResult( "nInserted" : 1 )>
db.info.find()
"_id" : 90, "name" : "blue", "age" : 30
"_id" : ObjectId("622070fb801321c032895aa9"), "name" : "itit", "age" : 28 >
db.info.insert([name:"zozo", age:25,name:"toto", age:100])#insert 插入多条数据
BulkWriteResult(
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
)>
db.info.find()
"_id" : 90, "name" : "blue", "age" : 30
"_id" : ObjectId("622070fb801321c032895aa9"), "name" : "itit", "age" : 28
"_id" : ObjectId("622070e3801321c032895aa7"), "name" : "zozo", "age" : 25
"_id" : ObjectId("622070e3801321c032895aa8"), "name" : "toto", "age" : 100 >
db.info.insertOne(name:"mike", age:33,name:"kiki", age:20)"acknowledged" : true,
"insertedId" : ObjectId("61d7ed25426f10e36ad4a89e")>
db.info.find()#可见 insertOne 一次插入多个数据的时候只有第一个生效
"_id" : 90, "name" : "blue", "age" : 30
"_id" : ObjectId("61d7ed25426f10e36ad4a89e"), "name" : "mike", "age" : 33 #相同语句连续插入两次
>
db.info.insertOne(name:"coco", age:20)"acknowledged" : true,
"insertedId" : ObjectId("622068f5801321c032895a9a")>
db.info.insertOne(name:"coco", age:20)"acknowledged" : true,
"insertedId" : ObjectId("62206900801321c032895a9b")>
db.info.find()
"_id" : 90, "name" : "blue", "age" : 30
"_id" : ObjectId("61d7ed25426f10e36ad4a89e"), "name" : "mike", "age" : 33
"_id" : ObjectId("622068f5801321c032895a9a"), "name" : "coco", "age" : 20
"_id" : ObjectId("62206900801321c032895a9b"), "name" : "coco", "age" : 20 #见上,同一条文档可以多从连续插入不会报错, 会发现 ObjectId是连续的#一次性插入多条数据
>
db.info.insertMany([name:"yoyo", age:33,name:"bill", age:20])"acknowledged" : true,
"insertedIds" : [
ObjectId("61d7ed50426f10e36ad4a8a0"),
ObjectId("61d7ed50426f10e36ad4a8a1")
]>
db.info.find()
"_id" : 90, "name" : "blue", "age" : 30
"_id" : ObjectId("61d7eced426f10e36ad4a89d"), "name" : "kiki", "age" : 20
"_id" : ObjectId("622068f5801321c032895a9a"), "name" : "coco", "age" : 20
"_id" : ObjectId("62206900801321c032895a9b"), "name" : "coco", "age" : 20
"_id" : ObjectId("61d7ed50426f10e36ad4a8a0"), "name" : "yoyo", "age" : 33
"_id" : ObjectId("61d7ed50426f10e36ad4a8a1"), "name" : "bill", "age" : 20 2)save 方法db.Collectionname.save()###可插入单条 或者 多条数据(批量插入),具体看写多少条文档#单个插入,插入一个文档
>
db.info.save(name:"rose", age:22)
WriteResult( "nInserted" : 1 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 22 >
db.info.save(_id:80,name:"tom", age:35)
WriteResult( "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 80 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 22
"_id" : 80, "name" : "tom", "age" : 35 #再次插入指定 _id ,发现此 _id 已经存在,如果内容不一样 ,此时就是 更新文档数据了
>
db.info.save(_id:80,name:"tom", age:40)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 22
"_id" : 80, "name" : "tom", "age" : 40 #批量插入,多个文档>
db.class.find()
"_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan"
"_id" : ObjectId("61de533957216ed19d04e4af"), "name" : "rose", "age" : 22 >
db.class.save([name:"tim", age:40,name:"jack",age:35])
BulkWriteResult(
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
)>
db.class.find()
"_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan"
"_id" : ObjectId("61de533957216ed19d04e4af"), "name" : "rose", "age" : 22
"_id" : ObjectId("61de543e57216ed19d04e4b3"), "name" : "tim", "age" : 40
"_id" : ObjectId("61de543e57216ed19d04e4b4"), "name" : "jack", "age" : 35 2、文档的更新db.Collectionname.update()#只更新匹配到的第一个文档,不建议使用这此种形式写法
db.Collectionname.updateOne()#只能更新单条文档
db.Collectionname.updateMany()#多条文档全部更新>
db.info.find()
"_id" : ObjectId("61d7ea55426f10e36ad4a896"), "name" : "coco", "age" : 40
"_id" : ObjectId("61d7ea5a426f10e36ad4a897"), "name" : "coco", "age" : 60
"_id" : ObjectId("61d7ea62426f10e36ad4a898"), "name" : "coco", "age" : 30
"_id" : 13, "name" : "coco", "age" : 39
"_id" : 15, "name" : "coco", "age" : 39 >
db.info.updateOne(name:"coco",$set:age:10)
"acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 >
db.info.find()# 只更改了第一行的coco10
"_id" : ObjectId("61d7ea55426f10e36ad4a896"), "name" : "coco", "age" : 10
"_id" : ObjectId("61d7ea5a426f10e36ad4a897"), "name" : "coco", "age" : 60
"_id" : ObjectId("61d7ea62426f10e36ad4a898"), "name" : "coco", "age" : 30
"_id" : 13, "name" : "coco", "age" : 39
"_id" : 15, "name" : "coco", "age" : 39 >
db.info.updateOne(name:"coco",$set:age:10)
"acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 >
db.info.updateOne(name:"coco",$set:age:10)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )>
db.info.find()#上面再次执行多少次都不会报错,但是也不会更改,检测到 age 是 10 了
"_id" : ObjectId("61d7ea55426f10e36ad4a896"), "name" : "coco", "age" : 10
"_id" : ObjectId("61d7ea5a426f10e36ad4a897"), "name" : "coco", "age" : 60
"_id" : ObjectId("61d7ea62426f10e36ad4a898"), "name" : "coco", "age" : 30
"_id" : 13, "name" : "coco", "age" : 39
"_id" : 15, "name" : "coco", "age" : 39 >
db.info.updateMany(name:"coco",$set:age:10)
"acknowledged" : true, "matchedCount" : 5, "modifiedCount" : 4 >
db.info.find()#全部更改了
"_id" : ObjectId("61d7ea55426f10e36ad4a896"), "name" : "coco", "age" : 10
"_id" : ObjectId("61d7ea5a426f10e36ad4a897"), "name" : "coco", "age" : 10
"_id" : ObjectId("61d7ea62426f10e36ad4a898"), "name" : "coco", "age" : 10
"_id" : 13, "name" : "coco", "age" : 10
"_id" : 15, "name" : "coco", "age" : 10 3、文档删除db.Collectionname.deleteOne()#只能删除单条文档
db.Collectionname.deleteMany()#多条文档一起删除
db.Collectionname.remove()#删除单条(指定删除或者匹配删除)、删除多条(匹配删除)# 1)deleteOne 和deleteMany 方法>
db.class.find()
"_id" : 13, "name" : "coco", "age" : 39
"_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan"
"_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20
"_id" : ObjectId("61de6a0e4f8fa3f11da6cd85"), "name" : "ggg", "age" : 20
"_id" : ObjectId("61de68364f8fa3f11da6cd42"), "name" : "bob", "age" : 30
"_id" : ObjectId("61de6bcff2cb355cb7a46a15"), "name" : "rose", "age" : 60
"_id" : ObjectId("61de6bd0f2cb355cb7a46a16"), "name" : "rose", "age" : 60
"_id" : ObjectId("61de6bd1f2cb355cb7a46a17"), "name" : "rose", "age" : 60 #指定 _id 删除
>
db.class.deleteOne(_id:13)
"acknowledged" : true, "deletedCount" : 1 >
db.class.deleteOne(_id:ObjectId("61de6a0e4f8fa3f11da6cd85"))
"acknowledged" : true, "deletedCount" : 1 >
db.class.find()
"_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan"
"_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20
"_id" : ObjectId("61de68364f8fa3f11da6cd42"), "name" : "bob", "age" : 30
"_id" : ObjectId("61de6bcff2cb355cb7a46a15"), "name" : "rose", "age" : 60
"_id" : ObjectId("61de6bd0f2cb355cb7a46a16"), "name" : "rose", "age" : 60
"_id" : ObjectId("61de6bd1f2cb355cb7a46a17"), "name" : "rose", "age" : 60 #删除符合条件的第一个文档
>
db.class.deleteOne("name":"rose")
"acknowledged" : true, "deletedCount" : 1 >
db.class.find()
"_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan"
"_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20
"_id" : ObjectId("61de68364f8fa3f11da6cd42"), "name" : "bob", "age" : 30
"_id" : ObjectId("61de6bd0f2cb355cb7a46a16"), "name" : "rose", "age" : 60
"_id" : ObjectId("61de6bd1f2cb355cb7a46a17"), "name" : "rose", "age" : 60 #删除符合条件的所有文档
>
db.class.deleteMany("name":"rose")
"acknowledged" : true, "deletedCount" : 2 >
db.class.find()
"_id" : ObjectId("61dd25b187f250afc6d4f3e2"), "name" : "zhangsan"
"_id" : ObjectId("61de53da57216ed19d04e4b1"), "name" : "kite", "age" : 20
"_id" : ObjectId("61de68364f8fa3f11da6cd42"), "name" : "bob", "age" : 30#2) remove 方法>
db.info.find()
"_id" : 80, "name" : "mike", "age" : 30
"_id" : ObjectId("61d7a122426f10e36ad4a87d"), "name" : "jack", "age" : 20
"_id" : ObjectId("61d7a3c3426f10e36ad4a87e"), "name" : "bob", "age" : 16
"_id" : ObjectId("61d7a484426f10e36ad4a880"), "name" : "jane", "age" : 26
"_id" : ObjectId("61d7a484426f10e36ad4a881"), "name" : "kite", "age" : 21
"_id" : ObjectId("61d7a4c9426f10e36ad4a883"), "name" : "jane", "age" : 29
"_id" : ObjectId("61d7a4c9426f10e36ad4a884"), "name" : "kite", "age" : 21
"_id" : ObjectId("61d7a4c9426f10e36ad4a885"), "name" : "kite", "age" : 21 >
db.info.remove(_id:80)
WriteResult( "nRemoved" : 1 )>
db.info.remove(_id : ObjectId("61d7a122426f10e36ad4a87d"))
WriteResult( "nRemoved" : 1 )>
db.info.find()
"_id" : ObjectId("61d7a3c3426f10e36ad4a87e"), "name" : "bob", "age" : 16
"_id" : ObjectId("61d7a484426f10e36ad4a880"), "name" : "jane", "age" : 26
"_id" : ObjectId("61d7a484426f10e36ad4a881"), "name" : "kite", "age" : 21
"_id" : ObjectId("61d7a4c9426f10e36ad4a883"), "name" : "jane", "age" : 29
"_id" : ObjectId("61d7a4c9426f10e36ad4a884"), "name" : "kite", "age" : 21
"_id" : ObjectId("61d7a4c9426f10e36ad4a885"), "name" : "kite", "age" : 21 >
db.info.remove(name:"jane",age:26)
WriteResult( "nRemoved" : 1 )>
db.info.find()
"_id" : ObjectId("61d7a3c3426f10e36ad4a87e"), "name" : "bob", "age" : 16
"_id" : ObjectId("61d7a484426f10e36ad4a881"), "name" : "kite", "age" : 21
"_id" : ObjectId("61d7a4c9426f10e36ad4a883"), "name" : "jane", "age" : 29
"_id" : ObjectId("61d7a4c9426f10e36ad4a884"), "name" : "kite", "age" : 21
"_id" : ObjectId("61d7a4c9426f10e36ad4a885"), "name" : "kite", "age" : 21 >
db.info.remove(name:"kite",age:21)
WriteResult( "nRemoved" : 3 )### 条件匹配到了就都删除了,不关心 _id 号>
db.info.find()
"_id" : ObjectId("61d7a3c3426f10e36ad4a87e"), "name" : "bob", "age" : 16
"_id" : ObjectId("61d7a4c9426f10e36ad4a883"), "name" : "jane", "age" : 29 4、文档查询(这里指说明简单查询,复杂的查询中第二法部分)1) 全部查询
文档查询语法格式: db.Collectionname.find() >
db.fruit.find()
"_id" : ObjectId("61de7780f2cb355cb7a46a1c"), "name" : "apple", "price" : 8.5
"_id" : ObjectId("61de77a1f2cb355cb7a46a1d"), "name" : "banana", "price" : 3.5
"_id" : 80, "name" : "orange", "price" : 6.5
"_id" : 70, "name" : "pear", "price" : 6.8
"_id" : ObjectId("61de786ef2cb355cb7a46a1f"), "name" : "peach", "price" : 11.5
"_id" : ObjectId("61de78d9f2cb355cb7a46a20"), "name" : "grapes", "price" : 11.5
"_id" : ObjectId("61de7931f2cb355cb7a46a22"), "name" : "plum", "price" : 5.5 #转化一种输出方式,以格式化的形式输出信息
>
db.fruit.find().pretty()"_id" : ObjectId("61de7780f2cb355cb7a46a1c"),
"name" : "apple",
"price" : 8.5"_id" : ObjectId("61de77a1f2cb355cb7a46a1d"),
"name" : "banana",
"price" : 3.5"_id" : 80, "name" : "orange", "price" : 6.5
"_id" : 70, "name" : "pear", "price" : 6.8 "_id" : ObjectId("61de786ef2cb355cb7a46a1f"),
"name" : "peach",
"price" : 11.5"_id" : ObjectId("61de78d9f2cb355cb7a46a20"),
"name" : "grapes",
"price" : 11.5"_id" : ObjectId("61de7931f2cb355cb7a46a22"),
"name" : "plum",
"price" : 5.52) 指定条件查询
>
db.fruit.find(_id:80)
"_id" : 80, "name" : "orange", "price" : 6.5 >
db.fruit.find(_id:ObjectId("61de786ef2cb355cb7a46a1f"))
"_id" : ObjectId("61de786ef2cb355cb7a46a1f"), "name" : "peach", "price" : 11.5 >
db.fruit.find("name":"apple")
"_id" : ObjectId("61de7780f2cb355cb7a46a1c"), "name" : "apple", "price" : 8.5
四、关于 Insert 、Update 的使用说明
- 【Mongodb 数据库的简单使用】Mongodb 3.2 版本以前是没有 insertOne、insertMany、updateOne、updateMany 这种使用方法的,只有 Insert 、Update 的方法
- 关于 Insert 方法上面有具体介绍过的,下面就 3.2 版本之前的 Update 的语法作一个说明
#Update函数的语法格式
db.collection.update(query,obj,upsert,multi)
query:查询条件
obj:update对象和一些更新操作符
upsert:如果不存在update的记录,是否插入objNew这个新的文档,true为插入,默认为false,不插入。
multi: 默认是false,只更新找到的第一条记录。如果为true,把按条件查询出来的记录全部更新。###分下面几种情况:1、条件匹配到
第三个字段是 false ,即为默认状态 ,这时候第四个字段有两种情况
1)x=a,x,false,false匹配到的第一个 x=a 那条文档内容
2)x=a,x,false,true匹配到的所有 x=a 所有文档内容2、条件没匹配到
第三个字段是 true ,这个时候分四种情况:
1) x=a,x,true,false没有匹配到 x=a ,执行插入,这里是添加文档
x=a,x,true,true没有匹配到 x=a ,执行插入,这里是添加文档
2) x=a,x,true,flase匹配到 x=a ,执行更改,只更改匹配到的第一个文档内容
x=a,x,true,true匹配到 x=a ,执行更改,更改匹配到的 全部文档 的内容#使用示例
>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 22
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 22 #默认情况下修改了匹配到的第一个
>
db.info.update("name":"rose",$set:"age":40)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )>
db.info.update("name":"rose",$set:"age":40,false,false)#效果同上,默认情况下upsert 、multi都是false的 >
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 22 >
db.info.insert("name":"rose","age":22)
WriteResult( "nInserted" : 1 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 22
"_id" : ObjectId("61d7bc4f426f10e36ad4a888"), "name" : "rose", "age" : 22 >
db.info.insert("name":"rose")
WriteResult( "nInserted" : 1 )
>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 22
"_id" : ObjectId("61d7bc30426f10e36ad4a887"), "name" : "rose", "age" : 22
"_id" : ObjectId("61d7caf9426f10e36ad4a889"), "name" : "rose" #全部修改
>
db.info.update("name":"rose",$set:"age":40,false,true)
WriteResult( "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7bc30426f10e36ad4a887"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7caf9426f10e36ad4a889"), "name" : "rose", "age" : 40 #第三个字段是 true 时,分两种大情况四种具体情况#没匹配到的情况>
db.info.update("name":"jack",$set:"age":50,true,false)
WriteResult(
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("61d7d0e0b3e595522b7bd153")
)>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7bc30426f10e36ad4a887"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7caf9426f10e36ad4a889"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7d0e0b3e595522b7bd153"), "name" : "jack", "age" : 50 >
db.info.update("name":"mike",$set:"age":50,true,true)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7bc30426f10e36ad4a887"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7caf9426f10e36ad4a889"), "name" : "rose", "age" : 40
"_id" : ObjectId("61d7d0e0b3e595522b7bd153"), "name" : "jack", "age" : 50
"_id" : ObjectId("61d7d0es3e5sd22b7bd4a153"), "name" : "mike", "age" : 50 # 匹配到的情况>
db.info.update("name":"rose",$set:"age":99,true,true)
WriteResult( "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7bc30426f10e36ad4a887"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7bc4f426f10e36ad4a888"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7caf9426f10e36ad4a889"), "name" : "rose", "age" : 99 >
db.info.update("name":"rose",$set:"age":60,true,false)
WriteResult( "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 )>
db.info.find()
"_id" : ObjectId("61d7b15b426f10e36ad4a885"), "name" : "rose", "age" : 60
"_id" : ObjectId("61d7b1a9426f10e36ad4a886"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7bc30426f10e36ad4a887"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7bc4f426f10e36ad4a888"), "name" : "rose", "age" : 99
"_id" : ObjectId("61d7caf9426f10e36ad4a889"), "name" : "rose", "age" : 99
推荐阅读
- Hyperledger Fabric 2.x 自定义智能合约
- Linux之ping命令
- 为什么在数据驱动的路上,AB 实验值得信赖()
- #yyds干货盘点# Kubernetes 搞定网络原来可以如此简单((25))
- (准备工作)
- 2小时速学大数据编程语言 Scala 秘籍
- MySQL临时文件案例--binlog cache临时文件
- 自编码器
- SpringBoot整合Redis实现简单的Pub/Sub(发布/订阅)