基本概念

  • 生活中:仓库、架子、物品
  • 计算机:数据库(database)、集合(collection)、数据/文档(document)

查看数据库

语法:show databases

> show databases
admin  0.000GB
local  0.000GB
test   0.000GB

选择数据库

语法:use 数据库名称

mongodb collection 中文档嵌套 mongodb的collection_2d

查看集合

语法:show collections

创建集合

语法:db.createCollection(‘集合名’)

> db.createCollection('cl')
{ "ok" : 1 }
>
> show collections
cl
>
> db.createCollection('c2')
{ "ok" : 1 }
>
> show collections
c2
cl

删除集合

语法:db.集合名.drop()

> show collections
c2
cl
>
> db.c2.drop()
true
> show collections
cl

MongoDB文档删增修查(CURD)

C增

语法:db.集合名.insert(JSON数据)

说明:集合存在则直接插入数据,集合不存在则隐式创建后插入数据

练习:在test2数据库的c1集合中插入数据(姓名叫张三,年龄18岁)

use test2
db.c1.insert({uname:'张三',age:18})

注:对象的键统一不加引号,查看集合数据时系统会自动加上
    mongodb会给每条数据增加一个全球唯一的_id键(由时间戳、机器码、PID		和计数器码组合而成)
> use test2
switched to db test2
> db.c1.insert({uname:'张三',age:18})
WriteResult({ "nInserted" : 1 })
> db.c1.find()
{ "_id" : ObjectId("601266f4f2f580df9fcd2e6c"), "uname" : "张三", "age" : 18 }

思考1:是否可以自定义_id值?

可以,只需要给插入的JSON数据增加_id键即可覆盖(但强烈不推荐)

思考2:如何插入多条数据?

传递数组,数组中写一个个JSON数据

db.c1.insert([
	{unames:"z3",age:3},
	{unames:"l4",age:4},
	{unames:"w5",age:5},
])
> db.c1.insert([
... {unames:"z3",age:3},
... {unames:"l4",age:4},
... {unames:"w5",age:5},
... ])
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
> db.c1.find()
{ "_id" : ObjectId("601266f4f2f580df9fcd2e6c"), "uname" : "张三", "age" : 18 }
{ "_id" : ObjectId("6012696cf2f580df9fcd2e6d"), "unames" : "z3", "age" : 3 }
{ "_id" : ObjectId("6012696cf2f580df9fcd2e6e"), "unames" : "l4", "age" : 4 }
{ "_id" : ObjectId("6012696cf2f580df9fcd2e6f"), "unames" : "w5", "age" : 5 }

思考3:如何快速插入10条数据?

mongodb底层使用JS引擎实现,所以支持部分js语法,因此可以使用for循环

use test2
for(var i=1 ; i<=10 ; i++){
	db.c2.insert({name:"a"+i,age:i})
}

mongodb collection 中文档嵌套 mongodb的collection_mongodb_02

R查

语法:db.集合名.find(条件,[查询的列])

条件:
	查询所有数据			    {}或者不写
	查询age=6的数据	 	 	{age:6}
	查询age=6且sex=男的数据   {age:6,sex="男"}

查询的列:
	不写			查询全部列(字段)
	{age=1}		  只显示age列(字段)
	{age=0}		  除了age列(字段)都显示
	注:不管如何查询,系统自定义的_id都显示

练习1:查询所有数据

> db.c2.find()
{ "_id" : ObjectId("60126acaf2f580df9fcd2e70"), "name" : "a1", "age" : 1 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e71"), "name" : "a2", "age" : 2 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e72"), "name" : "a3", "age" : 3 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e73"), "name" : "a4", "age" : 4 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e74"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e75"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e76"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e77"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e78"), "name" : "a9", "age" : 9 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e79"), "name" : "a10", "age" : 10 }

练习2:只显示name字段

> db.c2.find({},{name:1})
{ "_id" : ObjectId("60126acaf2f580df9fcd2e70"), "name" : "a1" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e71"), "name" : "a2" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e72"), "name" : "a3" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e73"), "name" : "a4" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e74"), "name" : "a5" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e75"), "name" : "a6" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e76"), "name" : "a7" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e77"), "name" : "a8" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e78"), "name" : "a9" }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e79"), "name" : "a10" }

练习3:显示除name字段外的所有字段

> db.c2.find({},{name:0})
{ "_id" : ObjectId("60126acaf2f580df9fcd2e70"), "age" : 1 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e71"), "age" : 2 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e72"), "age" : 3 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e73"), "age" : 4 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e74"), "age" : 5 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e75"), "age" : 6 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e76"), "age" : 7 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e77"), "age" : 8 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e78"), "age" : 9 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e79"), "age" : 10 }

练习4:显示年龄大于5岁的数据

运算符

作用

$gt

大于

$gte

大于等于

$lt

小于

$lte

小于等于

$ne

不等于

$in

in

$nin

not in

语法:db.集合名.find({键:{运算符:值}})

> db.c2.find({age:{$gt:5}})
{ "_id" : ObjectId("60126acaf2f580df9fcd2e75"), "name" : "a6", "age" : 6 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e76"), "name" : "a7", "age" : 7 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e77"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e78"), "name" : "a9", "age" : 9 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e79"), "name" : "a10", "age" : 10 }

练习5:查询年龄是5岁、8岁、10岁的数据

> db.c2.find({age:{$in:[5,8,10]}})
{ "_id" : ObjectId("60126acaf2f580df9fcd2e74"), "name" : "a5", "age" : 5 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e77"), "name" : "a8", "age" : 8 }
{ "_id" : ObjectId("60126acaf2f580df9fcd2e79"), "name" : "a10", "age" : 10 }

U改

普通语法:db.集合名.update(条件,新数据[,是否新增,是否修改多条])

是否新增:条件匹配不到时是否插入,True插入,False不插入,默认为False
是否修改多条:匹配成功的数据是否都修改,True是,False只修改一条,默认为False

升级语法:

db.集合名.update(条件,新数据)
	新数据:{修改器:{键:值}}

修改器

作用

$inc

递增

$rename

重命名列

$set

修改列值

$unset

删除列

准备工作:

> for(var i=1;i<=10;i++){
... db.c3.insert({name:"zs"+i,age:i})
... }
WriteResult({ "nInserted" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs1", "age" : 1 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs2", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }

练习1:将{name:“zs1”}改为{name:“zs2”}

> db.c3.update({name:"zs1"},{name:"zs2"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs2" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs2", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }

发现:不是修改而是替换

解决:使用升级语法

> db.c3.update({name:"zs2"},{$set:{name:"zs22"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs22" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs2", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }
>
> db.c3.update({name:"zs2"},{$set:{name:"zs22"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs22" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs22", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }

练习3:给{name:“zs10”}的年龄加2岁或减2岁

> db.c3.update({name:"zs10"},{$inc:{age:2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs22" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs22", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 12 }
>
> db.c3.update({name:"zs10"},{$inc:{age:-2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs22" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs22", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }

练习3:修改器综合练习

插入数据:db.c4.insert({name:“张三”,age:50,who:“男”,other:“歪果仁”})

需求:

(1)name改为“神龙教主” (修改器:$set)

(2)age增加2 (修改器:$inc)

(3)who字段改为sex (修改器:$rename)

(4)other删除 (修改器:$unset)

语法分析:

db.c4.update({name:"张三"},{$set:{name:"神龙教主"}})
						  {$inc:{age:2}}
						  {$rename:{who:"sex"}}
                          {$unset:{other:True}}
                          
如何一次性写多个修改器
db.c4.update({name:"张三"},{
	$set:{name:"神龙教主"},
	$inc:{age:2},
	$rename:{who:"sex"},
	$unset:{other:true}
})
> db.c4.insert({name:"张三",age:50,who:"男",other:"歪果仁"})
WriteResult({ "nInserted" : 1 })
> db.c4.find()
{ "_id" : ObjectId("60127a48f2f580df9fcd2e87"), "name" : "张三", "age" : 50, "who" : "男", "other" : "歪果仁" }
> db.c4.update({name:"张三"},{
... $set:{name:"神龙教主"},
... $inc:{age:2},
... $rename:{who:"sex"},
... $unset:{other:true}
... })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.c4.find()
{ "_id" : ObjectId("60127a48f2f580df9fcd2e87"), "name" : "神龙教主", "age" : 52, "sex" : "男" }

练习4:验证最后两个参数(了解)

【验证】若匹配不到是否数据

> db.c3.update({name:"zs30"},{$set:{age:30}},true)
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 1,
        "nModified" : 0,
        "_id" : ObjectId("60127b763bae0acb5e6c7883")
})
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs22" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs22", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }
{ "_id" : ObjectId("60127b763bae0acb5e6c7883"), "name" : "zs30", "age" : 30 }

【验证】匹配成功的数据是否都修改

> db.c3.update({name:"zs22"},{$set:{name:"zs222"}},false,true)
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7a"), "name" : "zs222" }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs222", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }
{ "_id" : ObjectId("60127b763bae0acb5e6c7883"), "name" : "zs30", "age" : 30 }

D删

语法:db.集合名.remove(条件 [,是否删除一条])

注:是否删除一条,true为删除一条,false全部删除,默认为false。

> db.c3.remove({},true)
WriteResult({ "nRemoved" : 1 })
> db.c3.find()
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7b"), "name" : "zs222", "age" : 2 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7c"), "name" : "zs3", "age" : 3 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7d"), "name" : "zs4", "age" : 4 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7e"), "name" : "zs5", "age" : 5 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }
{ "_id" : ObjectId("60127b763bae0acb5e6c7883"), "name" : "zs30", "age" : 30 }
>  db.c3.remove({})
WriteResult({ "nRemoved" : 10 })
e7f"), "name" : "zs6", "age" : 6 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e80"), "name" : "zs7", "age" : 7 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e81"), "name" : "zs8", "age" : 8 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e82"), "name" : "zs9", "age" : 9 }
{ "_id" : ObjectId("601272d8f2f580df9fcd2e83"), "name" : "zs10", "age" : 10 }
{ "_id" : ObjectId("60127b763bae0acb5e6c7883"), "name" : "zs30", "age" : 30 }
>  db.c3.remove({})
WriteResult({ "nRemoved" : 10 })