行随心而动 發表於 2020-6-9 14:25:00

MongoDB知识点总结

<p><span style="font-size: 18px"><strong>一:MongoDB 概述<br><span style="font-size: 16px">&nbsp; &nbsp; 一、NoSQL 简介</span></strong></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;1. 概念:NoSQL(Not Only SQL的缩写),指的是非关系型数据库,是对不同于传统的关系型数据库的数据库管理系统的统称。用于超大规模数据的存储,数据存储不需要固定的模式,无需多余操作就可以横向扩展。</span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;2. 特点</span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1. 优点:具有高可扩展性、分布式计算、低成本、架构灵活且是半结构化数据,没有复杂的关系等。</span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2. 缺点:没有标准化、有限的查询功能、最终一致是不直观的程序等。</span></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;3. 分类</span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200603213143489-716337215.png" alt=""></span></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200603214035846-1797173013.png" alt="" width="1000"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;4. NoSQL 和 RDBMS 的对比</span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200603213453552-1058456939.png" alt=""></span></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 二、MongoDB 简介</strong></span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;1. 概念:MongoDB 是由C++语言编写的一个基于分布式文件存储的开源文档型数据库系统。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;2. 功能:JSON 文档模型、动态的数据模式、二级索引强大、查询功能、自动分片、水平扩展、自动复制、高可用、文本搜索、企业级安全、聚合框架MapReduce、大文件存储GridFS。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.&nbsp;面向集合文档的存储:适合存储Bson(json的扩展)形式的数据;</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.&nbsp;格式自由,数据格式不固定,生产环境下修改结构都可以不影响程序运行;</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.&nbsp;强大的查询语句,面向对象的查询语言,基本覆盖sql语言所有能力;</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4.&nbsp;完整的索引支持,支持查询计划;</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5.&nbsp;使用分片集群提升系统扩展性;</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;3. 适用场景</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.&nbsp;网站数据:Mongo非常适合实时的插入,更新与查询,并具备网站实时数据存储所需的复制及高度伸缩性。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.&nbsp;缓存:由于性能很高,Mongo也适合作为信息基础设施的缓存层。在系统重启之后,由Mongo搭建的持久化缓存层可以避免下层的数据源 过载。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.&nbsp;在高伸缩性的场景,用于对象及JSON数据的存储。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608141353079-1445323854.png" alt="" loading="lazy"></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;4. 数据类型</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/i-beta/1610676/201912/1610676-20191229152221846-1163233720.png" alt=""></span></p>
<p><span style="font-size: 16px">&nbsp;</span></p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 三、概念详解</strong></span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;1. 数据库:MongoDB 默认的数据库为"db",该数据库存储在data目录中。单个实例可以容纳多个独立的数据库,每一个都有自己的集合和权限,不同的数据库也放置在不同的文件中。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;2. 集合:集合就是 MongoDB 文档组,类似于 RDBMS 的表格。集合存在于数据库中,集合没有固定的结构,这意味着你在对集合可以插入不同格式和类型的数据,但通常情况下我们插入集合的数据都会有一定的关联性。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;3.<strong>&nbsp;</strong>文档:一个键值(key-value)对(即BSON)。MongoDB 的文档不需要设置相同的字段,并且相同的字段不需要相同的数据类型,这与关系型数据库有很大的区别,也是 MongoDB 非常突出的特点。</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2018.cnblogs.com/i-beta/1610676/201912/1610676-20191229151353167-2102860329.png" alt=""></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608200432272-414306608.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 四、安装配置</strong></span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;1. Windows</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1. 下载(4.2.7版本以上)并直接安装</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608190913464-2077186231.png" alt="" loading="lazy"></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2. 配置环境变量</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608191222606-1801459150.png" alt="" loading="lazy"></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3. 测试</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608191905909-222663497.png" width="600" loading="lazy"></p>
<p>=</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;2. Linux</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608200246950-1909240022.png" alt="" loading="lazy"></span></p>
<div class="cnblogs_code" style="position: relative; left: 62px">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 解压到指定目录</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>tar -zxvf mongodb-linux-x86_64-rhel70-4.2.7.tgz -C /opt/
<span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 配置环境变量</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> MONGODB_HOME</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>export MONGODB_HOME=/opt/mongodb-linux-x86_64-rhel70-4.2.7
<span style="color: rgba(0, 128, 128, 1)"> 7</span>export PATH=$PATH:$MONGODB_HOME/<span style="color: rgba(0, 0, 0, 1)">bin
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 测试</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> mongo</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>MongoDB shell version: 4.2.7
<span style="color: rgba(0, 128, 128, 1)">12</span>connecting to: test</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>二:MongoDB CLI&nbsp;</strong></span></p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 一、增删改</strong></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200608210707994-907141580.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 二、操作符</strong></span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200609081014876-233487052.png" alt="" loading="lazy"></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 三、查询</strong></span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;1. 基本操作</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200609092337769-577952097.png" alt="" loading="lazy"></span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;2. 聚合查询</span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200609091401492-1897775545.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;3.</span>&nbsp;<span style="font-size: 16px">管道操作:</span><span style="color: rgba(51, 51, 51, 1); font-family: -apple-system, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans SC&quot;, &quot;Noto Sans CJK SC&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif">MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200609090529403-448277064.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong><strong>&nbsp; &nbsp; 五、管理</strong></strong></span></p>
<p><span style="font-size: 16px"><strong><strong>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200609100256467-1727771408.png" alt="" loading="lazy"></strong></strong></span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/1610676/202006/1610676-20200609103237465-79217239.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong><strong>&nbsp; &nbsp; 六、索引和高可用</strong></strong></span></p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;1. 索引</span></p>
<div class="cnblogs_code" style="position: relative; left: 62px">
<pre><span style="color: rgba(0, 0, 0, 1)">简介
作用:索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录。索引主要用于排序和检索。
MongoDB使用 ensureIndex() 方法来创建索引,ensureIndex()方法基本语法格式如下所示:
db.collection.createIndex(keys, options)
语法中 </span><span style="color: rgba(0, 0, 255, 1)">Key</span> 值为要创建的索引字段,1为指定按升序创建索引,如果你想按降序来创建索引指定为<span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,也可以指定为hashed(哈希索引)。

索引属性:
    background:是否后台构建索引,在生产环境中,如果数据量太大,构建索引可能会消耗很长时间,为了不影响业务,可以加上此参数,后台运行同时还会为其他读写操作让路,这个建议配置为true开启,这样来提高效率。

    </span><span style="color: rgba(0, 0, 255, 1)">unique</span><span style="color: rgba(0, 0, 0, 1)">:是否为唯一索引

索引类型:
    单键索引:
      在某一个特定的属性上建立索引,例如:db.users. createIndex({age:</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">});
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,mongoDB在ID上建立了唯一的单键索引,所以经常会使用id来进行查询;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,在索引字段上进行精确匹配、排序以及范围查找都会使用此索引;

    复合索引:
      在多个特定的属性上建立索引,例如:db.users. createIndex({username:</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>,age:<span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>,country:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">});
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,复合索引键的排序顺序,可以确定该索引是否可以支持排序操作;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,在索引字段上进行精确匹配、排序以及范围查找都会使用此索引,但与索引的顺序有关;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">,为了性能考虑,应删除存在与第一个键相同的单键索引

    多键索引:
      在数组的属性上建立索引,例如:db.users. createIndex({favorites.city:</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">});

    哈希索引:
      不同于传统的B</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(0, 0, 0, 1)">树索引,哈希索引使用hash函数来创建索引。
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,在索引字段上进行精确匹配,但不支持范围查询,不支持多键hash;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,Hash索引上的入口是均匀分布的,在分片集合中非常有用;

优化
</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,开启内置的查询分析器,记录读写操作效率:
    db.setProfilingLevel(n,{m}),n的取值可选0,</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>,<span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">;
      0是默认值表示不记录;
      1表示记录慢速操作,如果值为1,m必须赋值单位为ms,用于定义慢速查询时间的阈值;
      2表示记录所有的读写操作;

</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,分析慢速查询
    就是查看执行计划,使用explain分析慢速查询。
    explain的入参可选值为:
      "queryPlanner":是默认值,表示仅仅展示执行计划信息;
      "executionStats":表示展示执行计划信息同时展示被选中的执行计划的执行情况信息;
      "allPlansExecution":表示展示执行计划信息,并展示被选中的执行计划的执行情况信息,还展示备选的执行计划的执行情况信息

</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">,合理建立索引
    建立索引的规则:
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,索引很有用,但是它也是有成本的——它占内存,让写入变慢;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,mongoDB通常在一次查询里使用一个索引,所以多个字段的查询或者排序需要复合索引才能更加高效;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">,复合索引的顺序非常重要,例如此脚本所示:
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span><span style="color: rgba(0, 0, 0, 1)">,在生成环境构建索引往往开销很大,时间也不可以接受,在数据量庞大之前尽量进行查询优化和构建索引;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">5</span><span style="color: rgba(0, 0, 0, 1)">,避免昂贵的查询,使用查询分析器记录那些开销很大的查询便于问题排查;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">6</span><span style="color: rgba(0, 0, 0, 1)">,通过减少扫描文档数量来优化查询,使用explain对开销大的查询进行分析并优化;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7</span><span style="color: rgba(0, 0, 0, 1)">,索引是用来查询小范围数据的,不适合使用索引的情况:
            </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,每次查询都需要返回大部分数据的文档,避免使用索引
            </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,写比读多

    优化目标:
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,根据需求建立索引
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,每个查询都要使用索引以提高查询效率, winningPlan. stage 必须为IXSCAN ;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span>,追求totalDocsExamined <span style="color: rgba(128, 128, 128, 1)">=</span> nReturned</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;2. 高可用</span></p>
<div class="cnblogs_code" style="position: relative; left: 62px">
<pre><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,可复制集
    可复制集是跨多个MongDB服务器(节点)分布和维护数据的方法。mongoDB可以把数据从一个节点复制到其他节点并在修改时进行同步,集群中的节点配置为自动同步数据;旧方法叫做主从复制,mongoDB </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">.0以后推荐使用可复制集;
    作用:
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,避免数据丢失,保障数据安全,提高系统安全性;
            (最少3节点,最大50节点)
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,自动化灾备机制,主节点宕机后通过选举产生新主机;提高系统健壮性;
            (7个选举节点上限)
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">,读写分离,负载均衡,提高系统性能;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span><span style="color: rgba(0, 0, 0, 1)">,生产环境推荐的部署模式;
    原理:
      数据同步:从节点与主节点保持长轮询;</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>.从节点查询本机oplog最新时间戳;<span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>.查询主节点oplog晚于此时间戳的所有文档;<span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">.加载这些文档,并根据log执行写操作;
      阻塞复制:与writeconcern相关,不需要同步到从节点的策略(如: acknowledged Unacknowledged 、w1),数据同步都是异步的,其他情况都是同步;
      心跳机制:成员之间会每2s 进行一次心跳检测(ping操作),发现故障后进行选举和故障转移;
      选举制度:主节点故障后,其余节点根据优先级和bully算法选举出新的主节点,在选出主节点之前,集群服务是只读的;
    注意:
      MongoDB复制集里Primary节点是不固定的,所以生产环境千万不要直连Primary。

</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,分片集群
    分片是把大型数据集进行分区成更小的可管理的片,这些数据片分散到不同的mongoDB节点,这些节点组成了分片集群。
    作用:
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,数据海量增长,需要更大的读写吞吐量:存储分布式
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,单台服务器内存、cpu等资源是有瓶颈的:负载分布式
    注意:分片集群是个双刃剑,在提高系统可扩展性和性能的同时,增大了系统的复杂性,所以在实施之前请确定是必须的。
    容易发生的状况:
      请求分流:通过路由节点将请求分发到对应的分片和块中;
      数据分流:内部提供平衡器保证数据的均匀分布,数据平均分布式请求平均分布的前提;
      块的拆分:</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">.4版本块的最大容量为64M或者10w的数据,当到达这个阈值,触发块的拆分,一分为二;
      块的迁移:为保证数据在分片节点服务器分片节点服务器均匀分布,块会在节点之间迁移。一般相差8个分块的时候触发;
      
    分片注意点:
      热点 :某些分片键会导致所有的读或者写请求都操作在单个数据块或者分片上,导致单个分片服务器严重不堪重负。自增长的分片键容易导致写热点问题;
      不可分割数据块:过于粗粒度的分片键可能导致许多文档使用相同的分片键,这意味着这些文档不能被分割为多个数据块,限制了mongoDB均匀分布数据的能力;
      查询障碍:分片键与查询没有关联,造成糟糕的查询性能。
    建议:
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,不要使用自增长的字段作为分片键,避免热点问题;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,不能使用粗粒度的分片键,避免数据块无法分割;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">,不能使用完全随机的分片键值,造成查询性能低下;
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span><span style="color: rgba(0, 0, 0, 1)">,使用与常用查询相关的字段作为分片键,而且包含唯一字段(如业务主键,id等);
      </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">5</span>,索引对于分区同样重要,每个分片集合上要有同样的索引,分片键默认成为索引;分片集合只允许在id和分片键上创建唯一索引;</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18px"><strong>三:MongoDB API</strong></span></p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 一、客户端连接</strong></span></p>
<div class="cnblogs_code" style="position: relative; left: 54px">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoDBClient {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> Logger logger = Logger.getLogger(MongoDBClient.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">.getName());
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>   <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> MongoClient mongoClient;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 128, 0, 1)">   * 封装MongoDB连接
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 128, 0, 1)">   *
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 128, 0, 1)">   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> driver   远程连接URL
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 128, 0, 1)">   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> database   数据库
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 128, 0, 1)">   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> collection 集合
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 128, 0, 1)">   * </span><span style="color: rgba(128, 128, 128, 1)">@return</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>   <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">static</span> MongoCollection&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)"> getMongoClient(String driver, String database,
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">            String collection) {
</span><span style="color: rgba(0, 128, 128, 1)">15</span>         <span style="color: rgba(0, 0, 255, 1)">if</span> (mongoClient == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">16</span>             <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接到MongoDB客户端</span>
<span style="color: rgba(0, 128, 128, 1)">17</span>             mongoClient =<span style="color: rgba(0, 0, 0, 1)"> MongoClients.create(driver);
</span><span style="color: rgba(0, 128, 128, 1)">18</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (mongoClient != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">19</span>               logger.info("mongoClient init success!"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">20</span>               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接指定数据库</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>               MongoDatabase db =<span style="color: rgba(0, 0, 0, 1)"> mongoClient.getDatabase(database);
</span><span style="color: rgba(0, 128, 128, 1)">22</span>               <span style="color: rgba(0, 0, 255, 1)">if</span> (database != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">23</span>                     logger.info("正在使用" + database + "数据库"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">24</span>                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 连接到指定集合</span>
<span style="color: rgba(0, 128, 128, 1)">25</span>                     <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> db.getCollection(collection);
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">                }
</span><span style="color: rgba(0, 128, 128, 1)">27</span>             } <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">28</span>               logger.info("mongoClient init failed!"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">31</span>         <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">33</span> }</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 二、增删改</strong></span></p>
<div class="cnblogs_code" style="position: relative; left: 54px">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoDBClient {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 128, 0, 1)">   * 插入文档
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">    @Test
</span><span style="color: rgba(0, 128, 128, 1)"> 6</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)"> insert() {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>         MongoCollection&lt;Document&gt; collection = getMongoClient("mongodb://admin:000000@localhost:27017"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>               "studentmanager", "info"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>         <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 128, 0, 1)">         * 插入一条
</span><span style="color: rgba(0, 128, 128, 1)">11</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 追加插入一条</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>         collection.insertOne(<span style="color: rgba(0, 0, 255, 1)">new</span> Document("name", "zhangsan").append("age", 18).append("gneder", "男"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 插入json数据</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>         String json = "{" + "'name':'lisi'" + "'age':'20'" + "'gender':'女'" + "}"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">      collection.insertOne(Document.parse(json));
</span><span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span>         <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 128, 0, 1)">         * 插入多条
</span><span style="color: rgba(0, 128, 128, 1)">21</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">22</span>         List&lt;Document&gt; documents = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Document&gt;<span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 128, 128, 1)">23</span>         <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i &lt; 4; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">24</span>             documents.add(<span style="color: rgba(0, 0, 255, 1)">new</span> Document("i"<span style="color: rgba(0, 0, 0, 1)">, i));
</span><span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 0, 1)">      collection.insertMany(documents);
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">28</span>
<span style="color: rgba(0, 128, 128, 1)">29</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 128, 0, 1)">   * 修改文档
</span><span style="color: rgba(0, 128, 128, 1)">31</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 0, 1)">    @Test
</span><span style="color: rgba(0, 128, 128, 1)">33</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() {
</span><span style="color: rgba(0, 128, 128, 1)">34</span>         MongoCollection&lt;Document&gt; collection = getMongoClient("mongodb://admin:000000@localhost:27017"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">35</span>               "studentmanager", "info"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改一条</span>
<span style="color: rgba(0, 128, 128, 1)">38</span>         collection.updateOne(<span style="color: rgba(0, 0, 255, 1)">new</span> Document("name", "zhangsan"), <span style="color: rgba(0, 0, 255, 1)">new</span> Document("$set", <span style="color: rgba(0, 0, 255, 1)">new</span> Document("age", 20<span style="color: rgba(0, 0, 0, 1)">)));
</span><span style="color: rgba(0, 128, 128, 1)">39</span>
<span style="color: rgba(0, 128, 128, 1)">40</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改多条</span>
<span style="color: rgba(0, 128, 128, 1)">41</span>         collection.updateMany(<span style="color: rgba(0, 0, 255, 1)">new</span> Document("i", <span style="color: rgba(0, 0, 255, 1)">new</span> Document("$lt", 2<span style="color: rgba(0, 0, 0, 1)">)),
</span><span style="color: rgba(0, 128, 128, 1)">42</span>               <span style="color: rgba(0, 0, 255, 1)">new</span> Document("$set", <span style="color: rgba(0, 0, 255, 1)">new</span> Document("name", "wangwu").append("age", 10<span style="color: rgba(0, 0, 0, 1)">)));
</span><span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">44</span>
<span style="color: rgba(0, 128, 128, 1)">45</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 128, 0, 1)">   * 删除文档
</span><span style="color: rgba(0, 128, 128, 1)">47</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 0, 1)">    @Test
</span><span style="color: rgba(0, 128, 128, 1)">49</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)"> delete() {
</span><span style="color: rgba(0, 128, 128, 1)">50</span>         MongoCollection&lt;Document&gt; collection = getMongoClient("mongodb://admin:000000@localhost:27017"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">51</span>               "studentmanager", "info"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">52</span>
<span style="color: rgba(0, 128, 128, 1)">53</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除一条</span>
<span style="color: rgba(0, 128, 128, 1)">54</span>         collection.deleteOne(<span style="color: rgba(0, 0, 255, 1)">new</span> Document("name", "zhangsan"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">55</span>
<span style="color: rgba(0, 128, 128, 1)">56</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除多条</span>
<span style="color: rgba(0, 128, 128, 1)">57</span>         collection.deleteMany(<span style="color: rgba(0, 0, 255, 1)">new</span> Document("i", <span style="color: rgba(0, 0, 255, 1)">new</span> Document("$gte", 2<span style="color: rgba(0, 0, 0, 1)">)));
</span><span style="color: rgba(0, 128, 128, 1)">58</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">59</span> }</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong>&nbsp; &nbsp; 三、查询</strong></span></p>
<div class="cnblogs_code" style="position: relative; left: 54px">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoDBClient {
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 128, 0, 1)">   * 查询所有
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">    @Test
</span><span style="color: rgba(0, 128, 128, 1)"> 6</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)"> findAll() {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span>         MongoCollection&lt;Document&gt; collection = getMongoClient("mongodb://admin:000000@localhost:27017"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>               "studentmanager", "info"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取迭代器</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>         FindIterable&lt;Document&gt; find =<span style="color: rgba(0, 0, 0, 1)"> collection.find();
</span><span style="color: rgba(0, 128, 128, 1)">11</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取游标</span>
<span style="color: rgba(0, 128, 128, 1)">12</span>         MongoCursor&lt;Document&gt; iterator =<span style="color: rgba(0, 0, 0, 1)"> find.iterator();
</span><span style="color: rgba(0, 128, 128, 1)">13</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过游标遍历检索为文档集合</span>
<span style="color: rgba(0, 128, 128, 1)">14</span>         <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator.hasNext()) {
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">            System.out.println(iterator.next());
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 128, 0, 1)">   * 条件查询
</span><span style="color: rgba(0, 128, 128, 1)">21</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 0, 1)">    @Test
</span><span style="color: rgba(0, 128, 128, 1)">23</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)"> findByConditions() {
</span><span style="color: rgba(0, 128, 128, 1)">24</span>         MongoCollection&lt;Document&gt; collection = getMongoClient("mongodb://admin:000000@localhost:27017"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">25</span>               "studentmanager", "info"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">26</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 范围查询
</span><span style="color: rgba(0, 128, 128, 1)">27</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> where gender = 男</span>
<span style="color: rgba(0, 128, 128, 1)">28</span>         collection.find(Filters.eq("gender", "男"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">29</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> where age=10,age=18,age=20</span>
<span style="color: rgba(0, 128, 128, 1)">30</span>         collection.find(Filters.in("age", 10, 18, 20<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">31</span>
<span style="color: rgba(0, 128, 128, 1)">32</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 逻辑查询
</span><span style="color: rgba(0, 128, 128, 1)">33</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> where age &gt;= 16 and age &lt;= 20</span>
<span style="color: rgba(0, 128, 128, 1)">34</span>         collection.find(Filters.and(Filters.gte("age", 16), Filters.lte("age", 20<span style="color: rgba(0, 0, 0, 1)">)));
</span><span style="color: rgba(0, 128, 128, 1)">35</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> where age &gt; 20 or age &lt; 16</span>
<span style="color: rgba(0, 128, 128, 1)">36</span>         collection.find(Filters.or(Filters.gt("age", 20), Filters.lt("age", 16<span style="color: rgba(0, 0, 0, 1)">)));
</span><span style="color: rgba(0, 128, 128, 1)">37</span>
<span style="color: rgba(0, 128, 128, 1)">38</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 类型查询</span>
<span style="color: rgba(0, 128, 128, 1)">39</span>         collection.find(Filters.type("name", "string"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">40</span>
<span style="color: rgba(0, 128, 128, 1)">41</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 模糊查询:name中是以zhang开头的</span>
<span style="color: rgba(0, 128, 128, 1)">42</span>         FindIterable&lt;Document&gt; find = collection.find(Filters.regex("name", "zhang*"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">43</span>         MongoCursor&lt;Document&gt; iterator =<span style="color: rgba(0, 0, 0, 1)"> find.iterator();
</span><span style="color: rgba(0, 128, 128, 1)">44</span>         <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator.hasNext()) {
</span><span style="color: rgba(0, 128, 128, 1)">45</span>             Document document =<span style="color: rgba(0, 0, 0, 1)"> iterator.next();
</span><span style="color: rgba(0, 128, 128, 1)">46</span>             System.out.println(document.get("name"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">49</span>
<span style="color: rgba(0, 128, 128, 1)">50</span>   <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">51</span> <span style="color: rgba(0, 128, 0, 1)">   * 排序和分页
</span><span style="color: rgba(0, 128, 128, 1)">52</span>      <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">53</span> <span style="color: rgba(0, 0, 0, 1)">    @Test
</span><span style="color: rgba(0, 128, 128, 1)">54</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)"> sortAndLimit() {
</span><span style="color: rgba(0, 128, 128, 1)">55</span>         MongoCollection&lt;Document&gt; collection = getMongoClient("mongodb://admin:000000@localhost:27017"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">56</span>               "studentmanager", "info"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">57</span>
<span style="color: rgba(0, 128, 128, 1)">58</span>         <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">59</span> <span style="color: rgba(0, 128, 0, 1)">         * 排序
</span><span style="color: rgba(0, 128, 128, 1)">60</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">61</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 升序排序:where gender="男" order by age asc</span>
<span style="color: rgba(0, 128, 128, 1)">62</span>         collection.find(Filters.eq("gender", "男")).sort(Sorts.ascending("age"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">63</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 降序排序:where gender="男" order by age desc</span>
<span style="color: rgba(0, 128, 128, 1)">64</span>         collection.find(Filters.eq("gender", "男")).sort(Sorts.descending("age"<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">65</span>         <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 多条件排序:where gender="男" order by age asc and score desc</span>
<span style="color: rgba(0, 128, 128, 1)">66</span>         collection.find(Filters.eq("gender", "男"<span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 128, 128, 1)">67</span>               .sort(Sorts.orderBy(Sorts.ascending("age"), Sorts.descending("score"<span style="color: rgba(0, 0, 0, 1)">)));
</span><span style="color: rgba(0, 128, 128, 1)">68</span>
<span style="color: rgba(0, 128, 128, 1)">69</span>         <span style="color: rgba(0, 128, 0, 1)">/**</span>
<span style="color: rgba(0, 128, 128, 1)">70</span> <span style="color: rgba(0, 128, 0, 1)">         * 分页
</span><span style="color: rgba(0, 128, 128, 1)">71</span>          <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">72</span>         FindIterable&lt;Document&gt; skip = collection.find(Filters.gt("age", 16)).limit(3).skip(2<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">73</span>         MongoCursor&lt;Document&gt; iterator =<span style="color: rgba(0, 0, 0, 1)"> skip.iterator();
</span><span style="color: rgba(0, 128, 128, 1)">74</span>         <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (iterator.hasNext()) {
</span><span style="color: rgba(0, 128, 128, 1)">75</span> <span style="color: rgba(0, 0, 0, 1)">            System.out.println(iterator.next());
</span><span style="color: rgba(0, 128, 128, 1)">76</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">77</span> <span style="color: rgba(0, 0, 0, 1)">    }
</span><span style="color: rgba(0, 128, 128, 1)">78</span> }</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/mh20131118/p/12116138.html
頁: [1]
查看完整版本: MongoDB知识点总结