MongoDB学习笔记
阅读目录
Nosql 简介:
NoSQL(Not Only SQL),意即“不仅仅是 SQL“,指的是非关系型数据库。是一种全新的数据库革命性运动,早期就有人提出,发展至 2009年趋勢越发高涨。NoSQL 的拥护者们提倡运用非关系型的数据存储,相对于铺天盖地的关系型数据库运用,这一概念无疑是种全新的思维的注入。
常见的 NoSql(非关系型数据库)数据库
NoSql 数据库优缺点
在优势方面主要体现在下面几点:
简单的扩展在不足方面主要有下面几点:
不提供对 SQL 的支持MongoDB 简介
MongoDB 是用 C++ 语言编写的非关系型数据库。特点是高性能、易部署、易使用,存储数据十分方便,主要特性有: 面向集合存储,易于存储对象类型的数据模式自由支持动态查询
支持完全索引,包含内部对象支持复制和故障恢复
使用高效的二进制数据存储,包括大型对象文件存储格式为 BSON(一种 JSON 的扩展)
1. 关系型数据的表的 rcord 必须保证拥有每一个 field
2.mongoDB 的每一个 document 的 key 可以不一样
3. 关系型数据查询使用 SQL
4.mongoDB 查询使用内置 find 函数 --->>> 基于 BSON 的特有查询工具
1. 逻辑结构关系的对比:
MongoDB 基本概念介绍:
集合(collection) 就是一组文档,
MongoDB 的单个计算机可以容纳多个独立的数据库,每一个数据库都有自己的集合和权限
MongoDB 自带简洁但功能强大的JavaSariptshell,这个工具对于管理 MongoDB 实例和操作数据作用非常大
每一个文档都有一一个特殊的键 id", 它在文档所处的集合中是唯一的,相当于关系数据库中的表的主键
MongoDB 的安装:
本机环境:
ubuntu18.04
sudo apt-get install mongodb
启动:
默认端口:27017
mongod
windows 平台:
连接:
mongo
MongoDB 数据类型:
MongoDB 常用操作:CRUD
mongo 的命令 连接到数据库
创建集合:
显示创建:db.createCollection("heima1")
隐式创建:直接在创建集合的同时往集合里添加数据
增删改查语法,过一遍即可
//更新 update tableName set "" where conditon
1: 用于设置更新的条件
2:用于设置更新的内容的对象
3:如果记录已经存在,更新它,否则新增一个记录,取值为 0 或 1
4:如果有多条记录被满足,是否全部更新.. (1 更新全部)(0 更新 1 条)
db.collectionName("condition","objectValue")
3: 建立索引的目的就是加速查询..
普通索引:
db.collectionName.ensureIndex({key:1})
唯一索引:
db.collectionName.ensureIndex({key:1},{unique:true})
在某一个 key 上面建立的唯一索引,这个 key 对应的值不能重复添加..
4: 固定集合:固定集合指的是事先创建而且大小固定的集合 。
固定集合很像环形队列,如果空间不足,最早的文档就会被删除,
为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制。
5:备份 与 恢复
//先把数据从数据当中通过命令导出来,然后我们删掉数据库当中的数据,再把导出来的数据恢复到数据库当中...
mongodb bin 目录下提供的命令
mongodump 备份
mongorestore 恢复..
6:导入导出
mongoexport
mongoimport
7: 数据库安全:
1:至少需要有个管理员账号,admin 数据库当中的用户都会被视为管理员
(连接到 admin 数据库当中,添加一个管理员账号)
2:我有了管理员的账号为其它的数据库分配账号(操作数据来说,读,写)
(为 test 分配读,写的账号)
3:重启我的 mongodb ,开启安全检查
4:客户端重新连接 mongdb ,接下来操作数据的部分都需要登录。。
5:集群
1:主从集群
2:副本集:副本集就是有自动故障恢复功能的主从集群。
主从集群和副本集最大的区别就是副本集没有固定的“主节点”
2.1:副本集当中最少保证要有两个节点
2.2:副本集当中的备份节点只做数据备份或者故障恢复的功能
6:分布式存储
分片 (sharding) 是指将数据拆分,将其分散存在不同的机器上的过程。有时也用分区 (partitioning) 来表示这个概念。将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存更多的数据,处理更多的负载。
重点理解片键
7:使用 java 代码 来调用 mongodb
连接其它的数据库的时候连接的驱动包
使用 mongodb 提供的客户端的 api
java nio netty mina
支持类似 js 语法输入
创建集合:
删除集合:
查看数据库信息:
查看集合:
条件表达式:
统计分页排序
查询包含关系:
$all
$in
$nin
$or
$exists
游标
更新集合:
例子:
$set
$inc
删除键:
索引:
目的:加速查询
新建索引:
固定集合:
capped collection:
固定集合:
固定集合指的是事先创建而且大小固定的集合 。固定集合很像环形队列,如果空间不足,最早的文档就会被删除,
为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制。
备份与恢复
备份:
恢复:
导入和导出
导出:
导入:
安全认证
安全认证
新版的 MongoDB 已经不支持 addUser 方法了:参考
db.createUser({user:'root',pwd:'123456',roles:['userAdminAnyDatabase']})
数据库安全:
1:至少需要有个管理员账号,admin 数据库当中的用户都会被视为管理员
(连接到 admin 数据库当中,添加一个管理员账号)
2:我有了管理员的账号为其它的数据库分配账号(操作数据来说,读,写)
(为 test 分配读,写的账号)
3:重启我的 mongodb ,开启安全检查
4:客户端重新连接 mongdb ,接下来操作数据的部分都需要登录。
MongoDB 的主重复制
主从复制:
操作:
MongoDB 的副本集
副本集:副本集就是有自动故障恢复功能的主从集群。
主从集群和副本集最大的区别就是副本集没有固定的“主节点”
2.1:副本集当中最少保证要有两个节点
2.2:副本集当中的备份节点只做数据备份或者故障恢复的功能
定义:
以三个节点为例:
启动:
初始化:
查询主库:
图示:
MongoDB 的分布式存储
分片(sharding)分布式存储
分片:
步骤:
添加数据:
图示
所用 JAVA 代码调用 MongoDB
import java.net.UnknownHostException;
import org.junit.Test;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
/
- 使用 java 代码调用 mongodb
- @author Administrator
*/
public class MongoDao {
@Test
public void add() throws UnknownHostException{
Mongo mongo=new Mongo("127.0.0.1", 27017);
DB db</span>=mongo.getDB(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
DBCollection collection</span>=db.getCollection(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">person</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">{key:value,key:value}</span>
BasicDBObject dbObject=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BasicDBObject();
dbObject.put(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">蒋珍珍</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
dbObject.put(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">age</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">18</span><span style="color: rgba(0, 0, 0, 1)">);
dbObject.put(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">sex</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">女</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
collection.insert(dbObject);
mongo.close();
}
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> del() throws UnknownHostException{
Mongo mongo</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> Mongo(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">127.0.0.1</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">27017</span><span style="color: rgba(0, 0, 0, 1)">);
DB db</span>=mongo.getDB(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
DBCollection collection</span>=db.getCollection(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">person</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">{key:value}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">{$nor:[{name:”user2”},{age:3}]
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">db.collection.find({age:{$gt:10}})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">{}</span>
DBObject dbObject=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BasicDBObject();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">dbObject.put("key",{}) {key:{}}</span>
collection.remove(dbObject);
mongo.close();
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> update(){
}
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> query() throws UnknownHostException{
Mongo mongo</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> Mongo(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">127.0.0.1</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">27017</span><span style="color: rgba(0, 0, 0, 1)">);
DB db</span>=mongo.getDB(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
DBCollection collection</span>=db.getCollection(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">person</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
// BasicDBObject dbObject=new BasicDBObject();
// dbObject.put("name", "蒋珍珍");
DBCursor dbCursor=collection.find();
</span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)">(dbCursor.hasNext()){
System.</span><span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)">.println(dbCursor.next().toString());
}
}
}
游戏的架构
为什么连接无需数据库驱动?