• MongoDB 3.2变动一览


    3.2测试版本总算release了!E叔带大家来一览MongoDB 3.2版本的真容。

    (PS:内容比较多,在此仅针对个人认为比较重要的进行讲解,markdown写的,貌似WP的markdown插件有点奇怪,格式出来和写的时候不太一样,有点丑,大家见谅。)

    以下是原文链接:

    Development Release Notes for 3.2.0 Release Candidate

    storage engine change

    • WiredTiger引擎将是默认引擎了.

      dbpath中有数据,在配置中没有指定引擎的话,会自动判断是什么引擎,但是如果指定了引擎,在dbpath中如果有别的引擎的数据文件,将不能启动。

    index change

    • 3.2将不允许version 0的indexes了,如果有会在log中warning(解决方式:删除index,重建index)

    aggregation compatibility changes

    • $avg 如果在不存在的字段上进行计算,会返回null(此前是0)

    • $substr 将报错,如果返回结果是无效的UTF-8(此前会返回该结果)

    • 数组元素将不会被像之前那样通过aggregation pipeline一个一个拆成为literal了,而是会直接解析为expression。

    • 如果希望还是用literal的方式,可以使用$literal

    Replication Election Enhancements

    复制集选举的加强。

    • 3.2后复制集的failover 时间和选举流程&算法有了很好的优化。(记得算法变动大致是投票的请求和返回都带上了一个类似version的,这样就不像以前默认投票一次后需要等30s)

      复制集默认使用protocolVersion: 1,而之前版本 protocolVersion: 0

      新增复制集配置参数settings.electionTimeoutMillis(默认10000(ms)),意思就不用解释了,顾名思义。(只在protocolVersion: 1的时候适用)

      新增复制集配置参数settings.heartbeatIntervalMillis(默认2000),顾名思义 心跳检测的timeout设置

    Sharded Cluster Enhancements

    shard集群的加强

    • 3.2版本不建议用3个mongod实例作为config server了。

      在3.2版本中,默认config servers 将以一组复制集的形式服务。(必须使用WT引擎),该变动增强了config servers的一致性,这样允许shard cluster可以拥有更多个数的config servers(比如up to 50:))

    • 同时config server 的repl set有如下限制:

      • 不能有投票节点
      • 不能有delay节点
      • 必须可以建立indexes(builindexes设置不能为false)

    部署新的config server的方式我就不一一搬运了url如下:

    新的config server 部署方式

    readConcern

    • 3.2 WT支持了readConcern,有Local(默认)和majority2个设定。

      local就和以前一样没啥好说。

      majority,将返回节点最新的且已经确认已经被写入其他majority节点的数据。

    不细说了,url为:

    readConcern细节

    Partial Indexes

    mongodb3.2支持我们对于某个collection进行Partial index建立。

    怎么理解呢,简单举个例子:

    我们希望只对rating大于5的document建立 一个联合索引:

    db.restaurants.createIndex(
       { cuisine: 1, name: 1 },
       { partialFilterExpression: { rating: { $gt: 5 } } }
    )
    
    • partialFilterExpression这个参数可以对我们mongodb所有类型的索引适用。

      参数细节

    • 那么Partial indexes也有一些限制。

      Mongo将不会对在不完整的结果集上进行query或者sort的操作适用partial indexes。

      举个例子

    • 建立了这样的index

    db.restaurants.createIndex(
       { cuisine: 1, name: 1 },
       { partialFilterExpression: { rating: { $gt: 5 } } }
    )
    
    • 这样的会使用partial index
    db.restaurants.find( { rating: 6 } )
    db.restaruants.find( { cuisine: "Italian", rating: { $gte: 8 } } )
    
    • 而这样的不会:
    db.restaurants.find( { rating: { $lt: 8 } } )
    db.restaruants.find( { cuisine: "Italian" } )
    

    很好理解吧。

    其他的限制:

    • 不能同时适用partialFilterExpression 和 sparse参数

    • 不能建立多个仅仅是filter expression不同的partial index

    • 复制集或者shard cluster 需要都是3.2才能适用。

    • _id不适用

    • shard key不能是partial indexes

    partial indexes 与sparseindex的对比:

    对比

    Document Validation

    • 3.2开始,mongodb支持在insert 和update的时候validate documents了。(Validation rules are specified on a per-collection basis)

      有这3个参数,

    • validator,

    • validationLevel:有off,strict,moderate级别(默认strict)()

    • validationAction:有error和warn(默认error)

    可以通过db.createCollection()和collMod来设定。

    ps:这类操作不支持指定expressions

    $geoNear, $near, $nearSphere, $text, $where.

    • 例子1

      建立新的collection并设置validation

      指定contact表的phone字段只能是string,或者email必须匹配如下规则,或者status必须为Unkown或者Incomplete

    db.createCollection( "contacts", {
       validator: { $or:
          [
             { phone: { $type: "string" } },
             { email: { $regex: /@mongodb.com$/ } },
             { status: { $in: [ "Unknown", "Incomplete" ] } }
          ]
       }
    } )
    

    这时候做这样的插入:

    db.contacts.insert( { name: "xyz", status: "A" } )
    

    返回是:

    WriteResult({
       "nInserted" : 0,
       "writeError" : {
          "code" : 121,
          "errmsg" : "Document failed validation"
       }
    })
    
    • 例子2

      为已有collection建立validation

    db.runCommand( {
       collMod: "contacts",
       validator: { $or: [ { phone: { $exists: true } }, { email: { $exists: true } } ] }
    } )
    

    查看表的validation规则

    db.getCollectionInfos( { name: "contacts" } )
    
    • validation的限制:

      不能对admin,local和config database的表做validation

      不能对system.*表做validation

    • Bypass Validation

      设置bypassDocumentValidation参数可以让下列命令bypass validation per operation

      • applyOps command

      • clone command and db.cloneDatabase() method

      • cloneCollection command and db.cloneCollection()

      • copydb command and db.copyDatabase() method

      • findAndModify command and db.collection.findAndModify() method

      • mapReduce command and db.collection.mapReduce() method

      • insert command

      • update command

      • $out for the aggregate command and db.collection.aggregate() method

    Left Outer Join(目前只有企业版)

    • MongoDB3.2的企业版本将提供 left outer join 功能的$lookup
      用法如下:
    {
       $lookup:
         {
           from: <collection to join>,
           localField: <fieldA>,
           foreignField: <fieldB>,
           as: <output array field>
         }
    }
    

    具体请参考:
    $lookup

    aggregation framework的提升

    New stages, accumulators, and expressions.

    一些原来只在$group中的expressions现在也可以在$project中使用。如:

    • $avg

    • $min

    • $max

    • $sum

    • $stdDevPop

    • $stdDevSamp

    shard cluster上的性能提升。

    • 如果pipeline第一步是在shard key上 开始$match,那么整个pipeline只在mathing的shard上进行,此前是split 然后merge(在primary shard)

    • 如果aggregation操作在多个shard 分片上进行(且不需要在primaryshard上进行,$out和$lookup需要在primary上进行),那么最后的结果可以route到其他shard分片上进行merge,而不再是只能在primaryshard上,造成primary shard 的overload了。

    有关aggregation更多细节请参考:

    参考

    MongoDB tools的提升

    • mongodump和mongorestore支持 archive file 和stdout/in

      通过–archive 参数,mongodump和mongorestore支持archive file 和stdout/in streams。

    例子:

    mongodump --archive=test.20150715.archive --db test
    
    mongodump --archive --db test --port 27017 | mongorestore --archive --port 27018
    
    mongorestore --archive=test.20150715.archive --db test
    
    • mongodump 和mongorestore支持compressed data

      通过–gzip参数,mongodump 和mongorestore支持 comressed data dumps。大大节省了空间。

    例子:

    mongodump --gzip --db test
    
    mongodump --archive=test.20150715.gz --gzip --db test
    
    mongorestore --gzip --db test
    
    mongorestore --gzip --archive=test.20150715.gz --db test
    

    Encrypted Storage Engine(只有企业版支持,不赘述了)

    General Enhancements

    • Diagnostic Data Capture

      为了方便mongodb 工程师对mongodb server 的分析,3.2开始会定时收集 server statistics到diagnostic中。

      默认是1s的间隔,可以通过diagnosticDataCollectionPeriodMillis.修改

      会在dbpath下简历 diagnostic.data 文件夹

      配置diagnostic文件的大小可以使用diagnosticDataCollectionFileSizeMB,

      配饰diagnostic文件夹的大小可以使用diagnosticDataCollectionDirectorySizeMB.

    • Geospatial Optimization

      地理位置我个人用到比较少,有需要的朋友可以去下列连接看看:

    Geo Optimization

    • Bit Test Query Operators

      MongoDB 3.2 provides new query operators to test bit values:

      • $bitsAllSet

      • $bitsAllClear

      • $bitsAnySet

      • $bitsAnyClear

      • SpiderMonkey JavaScript Engine

    详情参考:

    参考

    • mongo Shell and CRUD API

    详情参考:

    mongo shell and CRUD API

    • WiredTiger and fsyncLock

      3.2开始WT支持fsync来做一致性锁了,就像以前mmap的fsync。

    • Text Search Enhancements

      全文index的提升,不在这细说了,具体url:

    Text Search

    • Changes Affecting Compatibility

      3.2版本的一些变动可能会影响到兼容性,或者需要我们进行一些设置。具体可以参考:

    兼容性变动

    • Additional Information

      下列是3.2版本的其他的一些参考

    参考


    关于作者



    周李洋, 社区常用ID eshujiushiwo, Teambition运维总监 关注Mysql与MongoDB技术,数据架构,服务器架构,高效运维等。 mongo-mopre,mongo-mload作者,任CSDN mongodb版主,MongoDB上海用户组发起人, MongoDB官方翻译组核心成员,MongoDB中文站博主,MongoDB Contribution Award获得者, MongoDB Days Beijing 2014演讲嘉宾。 联系方式:378013446 MongoDB上海用户组:313290880 欢迎交流。

    转载请注明链接:http://www.mongoing.com/eshu_3.2

  • 相关阅读:
    httpclient的并发连接问题
    Java中使用多线程、curl及代理IP模拟post提交和get访问
    有强大的cURL,忘掉httpclient的吧!
    JavaScript正则表达式在不同浏览器中可能遇到的问题
    Meta对照表
    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
    虚拟机centos7服务器下,启动oracle11g数据库和关闭数据库
    Step 4: Install and Configure Databases
    docker 中部署一个springBoot项目
    docker保存对容器的修改
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/11226560.html
Copyright © 2020-2023  润新知