* 1. 简介
* 推荐理由
* MongoDB 是一个基于分布式的开源文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
* MongoDB是一个介于关系数据库和非关系数据库之间的产品, 是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。 Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
* 分布式优缺点
* 优点
* 可靠性(容错) :
* 分布式计算系统中的一个重要的优点是可靠性。一台服务器的系统崩溃并不影响到其余的服务器。
* 可扩展性:
* 在分布式计算系统可以根据需要增加更多的机器。
* 资源共享:
* 共享数据是必不可少的应用,如银行,预订系统。
* 灵活性:
* 由于该系统是非常灵活的,它很容易安装,实施和调试新的服务。
* 更快的速度:
* 分布式计算系统可以有多台计算机的计算能力,使得它比其他系统有更快的处理速度。
* 开放系统:
* 由于它是开放的系统,本地或者远程都可以访问到该服务。
* 更高的性能:
* 相较于集中式计算机网络集群可以提供更高的性能(及更好的性价比)。
* 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是 数据库/集合/文档

* 例子
* 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版本(这个步骤,不必练习!!)

* [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

* # 这是整了2个容器

* 或者用1个容器,直接1个容器里启动


* 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

* 创建个用户 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)

* MongoBooster 连接 MongoDB3.2版本没问题,其他高版本不支持。
* 使用工具 NoSQLBooster 连接 MongoDB
* 3.1 无认证方式的连接


* 3.2 有认证的连接

* 5\. MongoDB 查询介绍
* 认证、查询所有用户

* show dbs:显示数据库列表

* show collections:显示当前数据库中的集合(类似关系数据库中的表)



* # use admin:切换当前数据库至admin数据库,这和MySQL里面的意思一样

* show users:显示用户

* db.help() # 显示数据库操作命令

* Help查看命令提示

* help
* db.help();
* db.yourColl.help();
* db.youColl.find().help();
* rs.help();
* db.foo.help() # 显示集合操作命令,同样有很多的命令,foo指的是当前数据库下,一个叫foo的集合

* db.system.users.find():对于当前数据库中的 system.users 集合进行数据查找(由于没有条件,会列出所有数据)

* # select * from system.users;
* db.system.users.find( { user : 'root1' } ) # 对于当前数据库中的 system.users 集合进行查找,条件是数据中有一个属性叫user,且 user 的值为 'root1'

* 等同于Mysql中: select * from system.users where user = 'root1' ;
* db.col2.find({title:'NoSQL Overview'},) 与 db.col2.find({title:'NoSQL Overview'}) 查询结果都一样



* 插入数据



* // 插入数据时,name / 'name' / "name" 都是一样的
* insert VS save




* 集合中文档中的域可以多样化

* 查询去掉后的当前聚集集合中的某列的重复数据





* db.test.distinct("name");
* 会过滤掉name中的相同数据
* 相当于:select distict name from userInfo;
* 查询多个条件同时成立,等同于 SQL中 and


* // SQL中: select * from test where name = 'Lucky' and sex = 'Female' ;
* 查询age > 22的记录


* db.test.find({age: {$gt: 22}});
* 相当于:select * from userInfo where age >22;



* db.test.find({age:{$lt:20}})
* 相当于:select * from test where age <20;
* db.test.find({age: {$lte: 18}});

* db.test.find({age: {$gte: 18}});

* 相当于:select * from test where age >= 25;
* 20 >=age>=18


* db.test.find({age: {$gte: 18,$lte:20}});
* 等同于SQL: select * from test where age between 18 and 20 ;
* 不等于 $ne


* 在某范围内 $in


* 不在某范围内 $nin
* $mod 取模 // 查询所有 age % 10 == 3 的docs

* 模糊查询 查询 name 中包含 rek 的数据 // db.test.find({name:/rek/})

* 查询name中以 Dere 开头的 // db.test.find({name:/^Der/})


* 查询name中以 33 开头的 // db.test.find({name:/33$/})

* 查询指定列name、age数据 // db.test.find({}, {name: 1, age: 1});





* 查询指定列name、age数据, age > 18 // db.test.find({age: {$gt: 18}}, {name: 1, age: 1});

* 排序(按照年龄排序)



* 升序:db.test.find().sort({age: 1});
* 降序:db.test.find().sort({age: -1});
* 或



* 统计 count()

* 查询前几条 // limit(n)




* 大量造数

* 删除


* 删除集合test2 // db.test2.drop()
* 删除当前数据库


* 更新 // 不保留其它fields


* 更新 // 保留其它fields


* 更新 ? 新增




* 清空集合

* 聚合
* 聚合(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);


* db.col2.aggregate().group({_id:"$by_user",count:{$sum:1}}) //根据by_user分组,统计个数

* > db.col2.aggregate().group({_id:"$by_user",count:{$sum:"$likes"}}) //以by_user分组,求和likes的值

* > db.col2.aggregate().group({_id:null,count:{$sum:"$likes"}}) //不分组,只根据likes字段求和

* 表达式 描述 实例
* $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 // 复制当前行到下一行
网友评论