介绍
MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引.
安装
①安装MongoDB服务
[root@CentOS7 ~]# vi /etc/yum.repos.d/mongodb-org-4.4.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/6/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
[root@CentOS7 ~]# yum install -y mongodb-org
②运行MongoDB服务
[root@CentOS7 ~]# mongod --dbpath /root/mongodata --directoryperdb --syncdelay 10 --port 27017 --bind_ip CentOS7 --fork --syslog
about to fork child process, waiting until server is ready for connections.
forked process: 63591
child process started successfully, parent exiting
③连接MongoDB服务
[root@CentOS7 ~]# mongo --port 27017 --host CentOS7
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f07f72c9-1225-463d-b9da-1c466de31109") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2020-10-24T06:17:15.540-04:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2020-10-24T06:17:15.540-04:00: You are running this process as the root user, which is not recommended
2020-10-24T06:17:15.540-04:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
2020-10-24T06:17:15.541-04:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2020-10-24T06:17:15.541-04:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
2020-10-24T06:17:15.541-04:00: Soft rlimits too low
2020-10-24T06:17:15.541-04:00: currentValue: 1024
2020-10-24T06:17:15.541-04:00: recommendedMinimum: 64000
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.mycoll.find() list objects in collection mycoll
db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
>
常规脚本
数据库
①查看数据库
> show dbs | databases
admin 0.000GB
config 0.000GB
local 0.000GB
②切换/创建数据库
> use baizhi
switched to db baizhi
③查看当前数据库
> db
baizhi
④删除数据库
> db.dropDatabase()
{ "ok" : 1 }
集合|表
①创建表
1.创建表
> db.createCollection("t_user")
{ "ok" : 1 }
> show tables | collections
t_user
2.创建固定集合
> db.createCollection("t_hobby",{capped:true,size:1,max:3})
{ "ok" : 1 }
capped
表示固定集合;max
表示存储最大的元素数;size
表示集合的大小以字节为单位,一般在设计固定大小的集合的时候一旦数据达到size
限制,MongoDB会自动删除older的记录。
3.约束表
var options={validator:{ $and:[ {name:{$type:'string',$exists:true}},
{age:{$type:'int',$exists:true}},
{sex:{$type:'bool',$exists:true}},
{birthDay:{$type:'date',$exists:true}}
]}
}
> db.createCollection("t_user",options)
{ "ok" : 1 }
> db.t_user.insert({name:"zhangsan",age:NumberInt(18),sex:true,birthDay:new Date("1990-12-13")});
更多参考:https://docs.mongodb.com/manual/core/document-validation/
集合操作
①插入
> db.t_user.insertOne({id:1,name:'jiangzz'})
> db.t_user.insertMany([{id:2,name:'lisi'},{id:3,name:'wangwu'}])
> db.t_user.insert([{id:4,name:'zhaoliu'},{id:5,name:'win7'}])
注意:MongoDB的每条记录都有一个ObjectId,必须保持唯一。如果当用户不指定的时候由系统自动生成。
mongodb insert()和save()的相同点和区别
> db.t_user.insert({_id:1,name:"zs"})
> db.t_user.insert({_id:1,name:"lisi"})
> db.t_user.save({_id:1,name:"wangwu"})
> db.t_user.save({_id:1,name:"zhaoliu"})
总结:若新增的数据中存在主键 ,insert() 会提示错误,而save() 则更改原来的内容为新内容。若新增的数据中没有主键时,都会增加一条记录。
②更新
> db.t_user.updateOne({id:1},{$set:{salary:10000}})
> db.t_user.updateMany({id:{$gte:2}},{$set:{salary:10000}})
> db.t_user.replaceOne({id:1},{name:"jiangzz1"})
> db.t_user.update({id:1},{$set:{name:"jzz"}})
> db.t_user.update({name:"ww"},{$inc:{salary:1000}},{multi:true})
> db.t_user.update({id:1},{$inc:{salary:1000}},{multi:true})
> db.t_user.update({id:2},{$inc:{salary:1000}},{multi:true,upsert:true})
更多:https://docs.mongodb.com/manual/reference/operator/update/
③删除
> db.t_user.remove({salary:10000},{justOne:true})
等价
> db.t_user.deleteOne({salary:10000})
> db.t_user.remove({salary:10000})
等价
> db.t_user.deleteMany({salary:10000})
④查询
→查询Embedded/Nested文档
db.t_store.insert([{ item: "iphone6s", category: '手机', size: { h: 14, w: 21, type: "厘米" },status: "A" },
{ item: "笔记本", category:'电脑', size: { h: 8.5, w: 11, type: "英尺" }, status: "A" }
])
> db.t_store.find( { size: { w:21, h:14, type: "厘米" } } ) --错误
> db.t_store.find( { size: { h:14, w:21, type: "厘米" } } )
> db.t_store.find( { 'size.type': "厘米" } )
> db.t_store.find( { "size.h": { $lt: 15 } } )
→数组查询
db.t_store.insert([ { item: "iphone6s", category: '手机', color:["土豪金","玫瑰金","银灰色"]},
{ item: "笔记本", category:'电脑', color:["黑色","土豪金"]}
])
> db.t_store.find({color:"土豪金"});
> db.t_store.find({color:{$all:["土豪金","玫瑰金"]}});
> db.t_store.find({'color.1':"土豪金"})
> db.t_store.find({color:{$size:3}});
→查询数组内嵌文档
db.t_user.insert( [ { name: "张小三",age:20,friends:[{name:"李晓思",age:25},{name:"王小五",age:22}]},
{ name: "李晓思",age:25,friends:[{name:"张小三",age:20},{name:"温晓琪",age:30}]}
])
> db.t_user.find({'friends.0.name':'李晓思'})
> db.t_user.find({"friends.age":{$gt:25}})
> db.t_user.find({"friends":{$elemMatch:{age:{$gt:25}}}})
→Project Fields
> db.t_user.find({},{name:true,'friends':true})
> db.t_user.find({},{name:true,'friends.name':true})
→查询null和空值
db.t_null.insert([{_id:1,item:null}, {_id:2} ])
> db.t_null.find({item:null});
{ "_id" : 1, "item" : null } { "_id" : 2 }
> db.t_null.find( { item : { $type: 10 } } )
{ "_id" : 1, "item" : null }
> db.t_null.find( { item : {$exists: false } } )
{ "_id" : 2 }
> db.t_null.find( { item : {$exists:true} } )
{ "_id" : 1, "item" : null }
→特殊操作符
比较操作符:$eq、$gt、$gte、$lt、$lte、$ne、$in、$nin
逻辑操作符: $or、$and、$not、$nor
元素操作符:$exists、$type
求值搜索符:$mod、$regex、$where
数组查询符: $all、$elemMatch、$size
db.t_user.insert([
{ name: "张小三",age:20,friends:[{name:"李晓思",age:25},{name:"王小五",age:22}]},
{ name: "李晓思",age:25,friends:[{name:"张小三",age:20},{name:"温晓琪",age:30}]}
])
1.查询年龄大于20~30之间且薪资小于10000
> db.t_user.find({$and:[{age:{$gt:20,$lt:30}},{salary:{$gt:10000}}]})
2.查询年龄是奇数的用户:
> db.t_user.find({age:{$mod:[1,1]}})
3.使用where实现数据的age[10,30]区间数据
> db.t_user.find({$where:"return this.age>10 && this.age<30"})
4.查询姓张的用户
> db.t_user.find({name:{$regex:"^张"}})
5.查询朋友中有年龄大于25岁的
> db.t_user.find({"friends":{$elemMatch:{age:{$gt:25}}}})
→分页&排序 skip、limit、sort排序
> db.t_user.find().skip(1).limit(1)
> db.t_user.find().skip(0).limit(10).sort({age:1});
→ 聚合函数
db.t_user.insert([
{name:"张三",sex:true,class:"软开1班",age:28,salary:18000},
{name:"李四",sex:true,class:"软开2班",age:25,salary:15000},
{name:"王五",sex:false,class:"软开1班",age:35,salary:10000},
{name:"赵六",sex:true,class:"软开2班",age:20,salary:15000}
])
1.统计班级人员的平均薪资
> db.t_user.aggregate([{$group:{_id:'$class',avgSalary:{$avg:'$salary'}}}])
2.分析班级中薪资大于10000学生的平均年龄
> db.t_user.aggregate([{$match:{salary:{$gt:10000}}},{$group:{_id:'$class',avgAge:{$avg:'$age'}}}])
3.求每一个班中薪资最高薪资
> db.t_user.aggregate([{$group:{_id:'$class',maxSalary:{$max:'$salary'}}}])
4.求每一个班中薪资最低薪资
> db.t_user.aggregate([{$group:{_id:'$class',minSalary:{$min:'$salary'}}}])
5.求每一个班中薪资最低薪资班级降序
> db.t_user.aggregate([{$group:{_id:'$class',minSalary:{$min:'$salary'}}},{$sort:{_id:-1}}])
6.按照班级平均薪资降序
> db.t_user.aggregate([{$group:{_id:'$class',avgSalary:{$avg:'$salary'}}},{$sort:{avgSalary:-1}}])
7.求班级平均薪资最高的班级信息
> db.t_user.aggregate([{$group:{_id:'$class',avgSalary:{$avg:'$salary'}}},{$sort:{avgSalary:1}},{$limit:1},{$skip:1}])
MapReduce 统计参考:https://docs.mongodb.com/manual/aggregation/#map-reduce
→MongoDB 索引
1.单Field索引
> db.records.createIndex( { score: 1 } )
2.复合索引
> db.collection.createIndex({ score: 1,name:-1 } )
3.唯一索引
> db.members.createIndex({"user_id":1},{unique:true})
4.稀疏索引
> db.t_user.createIndex({name:1},{sparse:true})
5.部分索引 :
> db.t_user.createIndex({name:1}, {partialFilterExpression:{ age:{$gt:18} }, unique:true })
删除索引
> db.t_user.dropIndex({name:1})
> db.t_user.dropIndexes()
db.t_user.explain(“executionStats”)查看执行计划