* 1. 简介
* 推荐理由
* MongoDB 是一个基于分布式的开源文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
* MongoDB是一个介于关系数据库和非关系数据库之间的产品, 是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。 Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
* 分布式优缺点
* 优点
* 可靠性(容错) :
* 分布式计算系统中的一个重要的优点是可靠性。一台服务器的系统崩溃并不影响到其余的服务器。
* 可扩展性:
* 在分布式计算系统可以根据需要增加更多的机器。
* 资源共享:
* 共享数据是必不可少的应用,如银行,预订系统。
* 灵活性:
* 由于该系统是非常灵活的,它很容易安装,实施和调试新的服务。
* 更快的速度:
* 分布式计算系统可以有多台计算机的计算能力,使得它比其他系统有更快的处理速度。
* 开放系统:
* 由于它是开放的系统,本地或者远程都可以访问到该服务。
* 更高的性能:
* 相较于集中式计算机网络集群可以提供更高的性能(及更好的性价比)。
* 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是 数据库/集合/文档
![image](https://img.haomeiwen.com/i10517060/0523815f460967f1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 例子
* SQL
* id user_name email age
* 1 A ab@163.com 25
* 2 b bb@163.com 32
* MongoDB
* {
* "_id":ObjectId("1234567890123456789012f3"),
* "user_name":"A",
* "email":"ab@163.com"
* "age":25
* }
* {
* "_id":ObjectId("1234567890123456789012f2"),
* "user_name":"b",
* "email":"bb@163.com"
* "age":32
* }
* 2. Docker方式安装MongoDB 3.7.9版本(这个步骤,不必练习!!)
![image](https://img.haomeiwen.com/i10517060/061a658d15bb5f2a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* [https://hub.docker.com/_/mongo/](https://hub.docker.com/_/mongo/)# MongoDB Docker官网
* 按照提示,下拉想要的版本镜像
* Container shell access and viewing MongoDB logs
* $ docker exec -it some-mongo bash # 进入容器
* $ docker logs some-mongo # 查看容器启动日志
* 推荐启动Docker镜像方式
* (1) docker run -d --name some-mongo -v /home/jet/mongo_data_db:/data/db -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret mongo:3.7.9
* (2) docker run -it --rm --link some-mongo:mongo mongo mongo --host mongo -u mongoadmin -p secret --authenticationDatabase admin some-db
![image](https://img.haomeiwen.com/i10517060/e3f114f4caf0e861.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* # 这是整了2个容器
![image](https://img.haomeiwen.com/i10517060/9b88f7db96cdfe52.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 或者用1个容器,直接1个容器里启动
![image](https://img.haomeiwen.com/i10517060/17b9f0d872948c59.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/599242aaab8b30cd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 3\. Docker方式安装 MongoDB 3.2版本 ( 推荐安装这个版本)
* 运行如下命令,启动 MongoDB
* docker run --name some-mongo -v /home/jet/mongo_data_db/:/data/db -p 27017:27017 -d mongo:3.2
![image](https://img.haomeiwen.com/i10517060/cc9930c3eae85782.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 创建个用户 root2 / 123456 # 以后就需要用认证才能连接
* db.createUser({user:'root2',pwd:'123456',roles:[{role:'root',db:'admin'}]})
* # role:'root'为最高权限,可以在admin数据库中任何操作
* 4. 可视化工具连接MongoDB
* MongoBooster # 下载地址: [https://nosqlbooster.com/downloads](https://nosqlbooster.com/downloads)
![image](https://img.haomeiwen.com/i10517060/d0e11ee92272509e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* MongoBooster 连接 MongoDB3.2版本没问题,其他高版本不支持。
* 使用工具 NoSQLBooster 连接 MongoDB
* 3.1 无认证方式的连接
![image](https://img.haomeiwen.com/i10517060/6c3115102fcaa138.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/f95c2fe6ff9133d9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 3.2 有认证的连接
![image](https://img.haomeiwen.com/i10517060/d80f789a4cae41d7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 5\. MongoDB 查询介绍
* 认证、查询所有用户
![image](https://img.haomeiwen.com/i10517060/49533a1cd2678043.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* show dbs:显示数据库列表
![image](https://img.haomeiwen.com/i10517060/41ea3364a0527355.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* show collections:显示当前数据库中的集合(类似关系数据库中的表)
![image](https://img.haomeiwen.com/i10517060/aaf2ee90ce98b32c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/47c526b6fa56596a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/faccd4be045ef995.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* # use admin:切换当前数据库至admin数据库,这和MySQL里面的意思一样
![image](https://img.haomeiwen.com/i10517060/261ddf3de63c9fe9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* show users:显示用户
![image](https://img.haomeiwen.com/i10517060/1ca90b8560974836.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.help() # 显示数据库操作命令
![image](https://img.haomeiwen.com/i10517060/1d9380f667937cc3.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* Help查看命令提示
![image](https://img.haomeiwen.com/i10517060/3a72b18be075809d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* help
* db.help();
* db.yourColl.help();
* db.youColl.find().help();
* rs.help();
* db.foo.help() # 显示集合操作命令,同样有很多的命令,foo指的是当前数据库下,一个叫foo的集合
![image](https://img.haomeiwen.com/i10517060/c1f8770c64e942ea.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.system.users.find():对于当前数据库中的 system.users 集合进行数据查找(由于没有条件,会列出所有数据)
![image](https://img.haomeiwen.com/i10517060/d49b996ae90d2fa6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* # select * from system.users;
* db.system.users.find( { user : 'root1' } ) # 对于当前数据库中的 system.users 集合进行查找,条件是数据中有一个属性叫user,且 user 的值为 'root1'
![image](https://img.haomeiwen.com/i10517060/83fe31db9678d559.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 等同于Mysql中: select * from system.users where user = 'root1' ;
* db.col2.find({title:'NoSQL Overview'},) 与 db.col2.find({title:'NoSQL Overview'}) 查询结果都一样
![image](https://img.haomeiwen.com/i10517060/72ea36a98a4317d6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/46bc2883d3e43f1b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/c9221daebb089e42.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 插入数据
![image](https://img.haomeiwen.com/i10517060/338f2dab0a45b2fa.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/549348c4814cb334.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/e8a5d12032d068d1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* // 插入数据时,name / 'name' / "name" 都是一样的
* insert VS save
![image](https://img.haomeiwen.com/i10517060/0e7e18e5ec6832f7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/755be8d24991e4e0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/d386dd82c15eca3b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/d96f704ddf81622e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 集合中文档中的域可以多样化
![image](https://img.haomeiwen.com/i10517060/0f899998ce95be2d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 查询去掉后的当前聚集集合中的某列的重复数据
![image](https://img.haomeiwen.com/i10517060/59b07a6e6c094545.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/c63939c36216cc06.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/0b2957675969a0ca.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/c1498edef61a27bc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/da28f918fe7b02d6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.test.distinct("name");
* 会过滤掉name中的相同数据
* 相当于:select distict name from userInfo;
* 查询多个条件同时成立,等同于 SQL中 and
![image](https://img.haomeiwen.com/i10517060/4a1337010c0ca223.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/2b3bf96f99fb4c2f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* // SQL中: select * from test where name = 'Lucky' and sex = 'Female' ;
* 查询age > 22的记录
![image](https://img.haomeiwen.com/i10517060/a4764d80cb80e0cc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/3e39d846f7f07eba.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.test.find({age: {$gt: 22}});
* 相当于:select * from userInfo where age >22;
![image](https://img.haomeiwen.com/i10517060/2c900d2c5222cd86.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/f0d78f0d84b5db51.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/5e0837d54fecdc40.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.test.find({age:{$lt:20}})
* 相当于:select * from test where age <20;
* db.test.find({age: {$lte: 18}});
![image](https://img.haomeiwen.com/i10517060/237eb117b01f9936.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.test.find({age: {$gte: 18}});
![image](https://img.haomeiwen.com/i10517060/aa3241861bac1bfd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 相当于:select * from test where age >= 25;
* 20 >=age>=18
![image](https://img.haomeiwen.com/i10517060/1eaab96176df6ea7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/f8bd6e29e15b1cfb.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.test.find({age: {$gte: 18,$lte:20}});
* 等同于SQL: select * from test where age between 18 and 20 ;
* 不等于 $ne
![image](https://img.haomeiwen.com/i10517060/39449797579e5e61.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/269f094a111a4eac.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 在某范围内 $in
![image](https://img.haomeiwen.com/i10517060/b328a54a37b04899.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/df894dce68407285.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 不在某范围内 $nin
* $mod 取模 // 查询所有 age % 10 == 3 的docs
![image](https://img.haomeiwen.com/i10517060/4a195aa49d355ae4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 模糊查询 查询 name 中包含 rek 的数据 // db.test.find({name:/rek/})
![image](https://img.haomeiwen.com/i10517060/83c5ffb5bf8ed4e4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 查询name中以 Dere 开头的 // db.test.find({name:/^Der/})
![image](https://img.haomeiwen.com/i10517060/381e0331801d13b4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/77fbb046f174d772.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 查询name中以 33 开头的 // db.test.find({name:/33$/})
![image](https://img.haomeiwen.com/i10517060/c4cdce48f006ce15.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 查询指定列name、age数据 // db.test.find({}, {name: 1, age: 1});
![image](https://img.haomeiwen.com/i10517060/6be260470c76fb2e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/5f88b9641b5b37da.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/9d18afa51184c879.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/60ba8602d7ba4b87.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/d4adb3d266e89ef0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 查询指定列name、age数据, age > 18 // db.test.find({age: {$gt: 18}}, {name: 1, age: 1});
![image](https://img.haomeiwen.com/i10517060/b876a0a1d8d8ab05.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 排序(按照年龄排序)
![image](https://img.haomeiwen.com/i10517060/fe3e1e067c2bbe15.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/963054de9ed68356.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/176334ce3c7a3fc9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 升序:db.test.find().sort({age: 1});
* 降序:db.test.find().sort({age: -1});
* 或
![image](https://img.haomeiwen.com/i10517060/8b64d0275d87f5eb.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/72e3ccf5a543cad3.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/19650206fd210974.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 统计 count()
![image](https://img.haomeiwen.com/i10517060/3254ea39c103b4f8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 查询前几条 // limit(n)
![image](https://img.haomeiwen.com/i10517060/2abd3ef197f0360d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/04b5a08872d99989.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/028f76e8e884c5d0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/252989e00c23bb0e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 大量造数
![image](https://img.haomeiwen.com/i10517060/0a03f64d17e64fb9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 删除
![image](https://img.haomeiwen.com/i10517060/6b7b663b54232a4a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/f5a2e764d595307f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 删除集合test2 // db.test2.drop()
* 删除当前数据库
![image](https://img.haomeiwen.com/i10517060/92db5a2b073247dd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/c092acdf95153489.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 更新 // 不保留其它fields
![image](https://img.haomeiwen.com/i10517060/d71d45a934f34770.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/3bc368890c1869c1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 更新 // 保留其它fields
![image](https://img.haomeiwen.com/i10517060/f7bc6c188191528e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/3b983664e0fbbadc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 更新 ? 新增
![image](https://img.haomeiwen.com/i10517060/6a8e64b93776e3f1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/dbfcac438367cbd0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/dcb9653f0d2b9a90.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/1caa547629c5d91c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 清空集合
![image](https://img.haomeiwen.com/i10517060/2c13bf616a30a8a0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 聚合
* 聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。
* 例子
* 一次性插入3条数据
* var d;
* d=[{
* title: 'MongoDB Overview',
* description: 'MongoDB is no sql database',
* by_user: 'runoob.com',
* url: 'http://www.runoob.com',
* tags: ['mongodb', 'database', 'NoSQL'],
* likes: 100
* },
* {
* title: 'NoSQL Overview',
* description: 'No sql database is very fast',
* by_user: 'runoob.com',
* url: 'http://www.runoob.com',
* tags: ['mongodb', 'database', 'NoSQL'],
* likes: 10
* },
* {
* title: 'Neo4j Overview',
* description: 'Neo4j is no sql database',
* by_user: 'Neo4j',
* url: 'http://www.neo4j.com',
* tags: ['neo4j', 'database', 'NoSQL'],
* likes: 750
* }];
* db.col2.insert(d);
![image](https://img.haomeiwen.com/i10517060/a47f669a8861c479.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image](https://img.haomeiwen.com/i10517060/f19bc53a2fe5a292.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* db.col2.aggregate().group({_id:"$by_user",count:{$sum:1}}) //根据by_user分组,统计个数
![image](https://img.haomeiwen.com/i10517060/4fe0ec927b7b479c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* > db.col2.aggregate().group({_id:"$by_user",count:{$sum:"$likes"}}) //以by_user分组,求和likes的值
![image](https://img.haomeiwen.com/i10517060/a2e8fa138b33048f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* > db.col2.aggregate().group({_id:null,count:{$sum:"$likes"}}) //不分组,只根据likes字段求和
![image](https://img.haomeiwen.com/i10517060/405888a06456446f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 表达式 描述 实例
* $sum 计算总和。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
* $avg 计算平均值 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
* $min 获取集合中所有文档对应值得最小值。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
* $max 获取集合中所有文档对应值得最大值。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
* MongoBooster 常用快捷方式
* 选中 - F6 / 或者点击到哪一行,按F6,执行的是最后一行的语句
* Ctrl +T //打开shell
* Ctrl + R //刷新
* Ctrl + D // 复制当前行到下一行
网友评论