学习任务
- MongoDB基础Shell命令的学习和使用(下午)
参考:https://docs.mongodb.com/v3.6/tutorial/
数据库常规操作
> db #查看当前数据库
test
> use baizhi #切换|创建 数据库
switched to db baizhi
> show dbs|databases #查看所有数据
admin 0.000GB
baizhi 0.000GB
config 0.000GB
local 0.000GB
> db.dropDatabase() # 删除当前数据库
{ "dropped" : "baizhi", "ok" : 1 }
> db.help() #查看数据库的所有指令
集合的常规操作
普通集合
> db.createCollection("t_user")
{ "ok" : 1 }
> db.t_user.drop()
true
> db.t_user.insert({name:"zs"}) #隐式建表
WriteResult({ "nInserted" : 1 })
> > db.t_user.help() #查看表相关操作
固定集合
> db.createCollection("t_capped",{capped:true,size:1,max:3})
{ "ok" : 1 }
> db.t_capped.find({})
{ "_id" : ObjectId("5bbead83bdd6fb3edeecd6e1"), "name" : "zs" }
{ "_id" : ObjectId("5bbead85bdd6fb3edeecd6e2"), "name" : "ls" }
{ "_id" : ObjectId("5bbead89bdd6fb3edeecd6e3"), "name" : "ww" }
> db.t_capped.insert({name:"zl"})
WriteResult({ "nInserted" : 1 })
> db.t_capped.find({})
{ "_id" : ObjectId("5bbead85bdd6fb3edeecd6e2"), "name" : "ls" }
{ "_id" : ObjectId("5bbead89bdd6fb3edeecd6e3"), "name" : "ww" }
{ "_id" : ObjectId("5bbead9bbdd6fb3edeecd6e4"), "name" : "zl" }
capped必须设置为true,size设置集合的数据大小单位kb,max控制记录数,一旦操作记录数,就会产生覆盖。
创建约束集合(了解)
>db.createCollection("t_student",{ validator:{$and:[
{name:{$type:'string',$exists:true}},
{age:{$type:'int',$exists:true}},
{sex:{$type:'bool',$exists:true}},
{birthDay:{$type:'date',$exists:true}}
]}
});
> db.t_student.insert({name:"zs",age:NumberInt(18),sex:true,birthDay:new Date("1991-01-26")})
WriteResult({ "nInserted" : 1 })
MongoDB DML(重点)
插入
指令:insert、insertOne、insertMany、save
> db.t_user.insert({name:"zs",age:18})
> db.t_user.insertOne({name:"zs",age:18})
> db.t_user.insertMany([{name:"zs1",age:18},{name:"zs2",age:18}])
> db.t_user.insert([{name:"zs1",age:18},{name:"zs2",age:18}])
> db.t_user.save([{name:"zs11",age:18},{name:"zs22",age:18}])
> db.t_user.insert({_id:1,name:"lisi",age:18}) #不允许重复插入
> db.t_user.insert({_id:1,name:"lisi",age:18})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: baizhi.t_user index: _id_ dup key: { : 1.0 }"
}
})
> db.t_user.save({_id:1,name:"wangwu",age:20})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.t_user.save({_id:2,name:"zl",age:20})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2 })
查询(重点)
单个文档查询
[
{name:"zhangsan",age:18,sex:true,salary:15000},
{name:"lisi",age:20,sex:false,salary:8000},
{name:"wangwu",age:30,sex:true,salary:20000}
]
> var user=db.t_user.findOne({name:"lisi"}) #返回document
> var cursor=db.t_user.find({name:"lisi"}) #返回游标
> db.t_user.find({age:{$gt:18,$lte:20}})
> db.t_user.find({age:{$in:[18,20]}})
> db.t_user.find({$or:[{age:18},{name:"lisi"}]})
> db.t_user.find({name:{$not:{$eq:"zhangsan"}}})
内嵌文档查询
[
{name:"zhangsan",age:18,sex:true,salary:15000,address:{country:"中国",city:"北京"}},
{name:"lisi",age:20,sex:false,salary:8000,address:{country:"中国",city:"上海"}},
{name:"wangwu",age:30,sex:true,salary:20000,address:{country:"中国",city:"深圳"}}
]
> db.t_user.find({"address.city":{$in:["北京","上海"]}})
内嵌数组的查询
[
{name:"iPhoneX S",price:10000,colors:["银","红","黑","玫瑰"]},
{name:"iPhone6",price:10000,colors:["银","红","黑"]},
{name:"iPhone6s",price:10000,colors:["金","银","红","黑"]}
]
> db.t_store.find({colors:"黑"})
> db.t_store.find({colors:{$in:["黑","红","玫瑰"]}})#满足一个
> db.t_store.find({colors:{$all:["黑","红","玫瑰"]}})
> db.t_store.find({"colors.2":"黑"})
> db.t_store.find({$or:[{"colors":{$size:4}},{"colors":{$size:3}}]})
> db.t_store.find({"colors":{$size:{$gt:1,$lt:5}}}) #错误
内嵌数组文档查询
[
{
name:"zhangsan",
age:18,
orders:[
{id:1,name:"apple",price:4.5,count:2},
{id:2,name:"orange",price:2.5,count:2}
]
},
{
name:"lisi",
age:20,
orders:[
{id:1,name:"iphone",price:1500,count:1},
{id:2,name:"机械键盘",price:150,count:1}
]
}
]
> db.t_user.find({"orders.name":"apple"}).pretty()
> db.t_user.find({"orders.0.name":"apple"}).pretty()
> db.t_user.find({orders:{$elemMatch:{price:{$gte:100}}}}).pretty()
> db.t_user.find({orders:{$size:2}}).pretty()
空值查询
[
{name:"xxx1",sex:null},
{name:"xxx2"}
]
> db.t_user.find({sex:null})
{ "_id" : ObjectId("5bbec2debdd6fb3edeecd6ff"), "name" : "xxx1", "sex" : null }
{ "_id" : ObjectId("5bbec2debdd6fb3edeecd700"), "name" : "xxx2" }
> db.t_user.find({sex:{$type:"null"}})
{ "_id" : ObjectId("5bbec2debdd6fb3edeecd6ff"), "name" : "xxx1", "sex" : null }
> db.t_user.find({sex:{$exists:false}})
{ "_id" : ObjectId("5bbec2debdd6fb3edeecd700"), "name" : "xxx2" }
投影查询
> db.t_user.find({})
{ "_id" : ObjectId("5bbec391bdd6fb3edeecd701"), "name" : "zhangsan", "age" : 18, "sex" : true, "salary" : 15000 }
{ "_id" : ObjectId("5bbec391bdd6fb3edeecd702"), "name" : "lisi", "age" : 20, "sex" : false, "salary" : 8000 }
{ "_id" : ObjectId("5bbec391bdd6fb3edeecd703"), "name" : "wangwu", "age" : 30, "sex" : true, "salary" : 20000 }
> db.t_user.find({},{_id:false})
{ "name" : "zhangsan", "age" : 18, "sex" : true, "salary" : 15000 }
{ "name" : "lisi", "age" : 20, "sex" : false, "salary" : 8000 }
{ "name" : "wangwu", "age" : 30, "sex" : true, "salary" : 20000 }
> db.t_user.find({},{_id:false,name:true,age:true})
{ "name" : "zhangsan", "age" : 18 }
{ "name" : "lisi", "age" : 20 }
{ "name" : "wangwu", "age" : 30 }
正则查询
> db.t_user.find({name:{$regex:"zhan"}}) #检索name中含有zhan
> db.t_user.find({name:{$regex:"^zhan",$options: "i"}})#忽略大小写
where查询
> db.t_user.find({$and:[{age:{$gt:20}},{salary:{$gt:10000}}]})
> db.t_user.find({$where:function(){
return this.age> 20 && this.salary > 10000
}})
分页&排序
> db.t_user.find({}).skip(2).limit(4)
> db.t_user.find({}).skip(2).limit(4).size() # 返回游标大小,受分页参数影响
> db.t_user.find({}).skip(2).limit(4).count() # 返回的是查询匹配的记录总条数
> db.t_user.find({}).skip(0).limit(4).sort({age:1,name:1}) # 按照age升序name升序
更新
参考https://docs.mongodb.com/v3.6/reference/operator/update/
单个文档
[
{name:"zhangsan",age:18,sex:true,salary:15000},
{name:"lisi",age:20,sex:false,salary:8000},
{name:"wangwu",age:30,sex:true,salary:20000}
]
> db.t_user.update({sex:true},{$inc:{salary:500}}) # 更新1条等价updateOne
> db.t_user.update({sex:true},{$set:{salary:500}},{multi:true})
> db.t_user.updateMany({sex:true},{$set:{salary:500}},{multi:true})
> db.t_user.update({},{$unset:{salary:true}},{multi:true})
> db.t_user.update({},{$currentDate:{birthDay:true}},{multi:true})
> db.t_user.update({},{$currentDate:{birthDay:{$type:"date"}}},{multi:true})
> db.t_user.update({},{$rename:{birthDay:"生日"}},{multi:true})
数组更新
[
{name:"iPhoneX S",price:10000,colors:["银","红","黑","玫瑰"]},
{name:"iPhone6",price:10000,colors:["银","红","黑"]},
{name:"iPhone6s",price:10000,colors:["金","银","红","黑"]}
]
> db.t_store.update({name:"iPhone6s"},{$pull:{colors:"黑"}}) # 删除指定元素
> db.t_store.update({name:"iPhone6s"},{$pop:{colors:1}}) #后删
> db.t_store.update({name:"iPhone6s"},{$push:{colors:"黑"}}) #添加元素
> db.t_store.update({name:"iPhone6s"},{$push:{colors:{$each:["金","黑"]}}}) #默认追加
> db.t_store.update({name:"iPhone6s"},{$push:{colors:{$each:["金","黑"],$position:1}}}) #指定位置
> db.t_store.update({name:"iPhone6s"},{$addToSet:{colors:{$each:["金","黑","粉"]}}})
> db.t_store.update({name:"iPhone6s","colors":"黑"},{$set:{"colors.$":"黑色"}})#将黑改为黑色
内嵌文档
[
{name:"zhangsan",age:18,sex:true,salary:15000,address:{country:"中国",city:"北京"}},
{name:"lisi",age:20,sex:false,salary:8000,address:{country:"中国",city:"上海"}},
{name:"wangwu",age:30,sex:true,salary:20000,address:{country:"中国",city:"深圳"}}
]
> db.t_user.update({name:"lisi"},{$set:{"address.city":"北京市"}})
> db.t_user.update({name:"lisi"},{$unset:{"address.city":true}})
> db.t_user.update({name:"lisi"},{$set:{"address.zipcode":10010}})
内嵌数组文档
[
{
name:"zhangsan",
age:18,
orders:[
{id:1,name:"apple",price:4.5,count:2},
{id:2,name:"orange",price:2.5,count:2}
]
},
{
name:"lisi",
age:20,
orders:[
{id:1,name:"iphone",price:1500,count:1},
{id:2,name:"机械键盘",price:150,count:1}
]
}
]
> db.t_user.update({name:"zhangsan","orders.id":1},{$set:{"orders.$.price":5.0}})
> db.t_user.update({name:"zhangsan"},{$pull:{orders:{id:1}}})
> db.t_user.update({name:"zhangsan","orders.id":2},{$set:{"orders.$.describe":"...."}})