码迷,mamicode.com
首页 > 数据库 > 详细

Mongodb笔记(二) Index

时间:2017-01-13 13:52:00      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:efault   写入   text   get   选择   ted   大于   build   英语   

版本:mongodb3.4;

Index  :

  如果mongodb不能使用索引进行排序,就会将数据放入内存中进行排序,而当内存使用超过32MB时,就会报错。

  在创建索引时,应确保索引的选择力,避免多余的查询。避免没有选择力的索引。

  _id是默认的索引。

  基本方法:

      db.collection.createIndex({A:1});

      db.collection.dropIndex({A:1});       //两种方法删除。

             .dropIndex(‘indexName‘);

      db.collection.dropIndexes();

      db.collection.getIndexes();        

      db.collection.reIndex();            //重建index

  

  Single Indexes单项索引:

    {field:1}

  Compound Indexes复合索引:

    {A:1,B:1}

    两个field都建立索引。

    查询:  find ( { A: value})                      //使用index;

        find(  { A:value, B:value})           //使用Index;

       find ( { }) .sort({B:1})       // nonuse; 复合索引单独使用一个时,只认第一个(A)。此处使用索引交集,可以成功。

         find( { } ).sort ({A:1})              //use

        .sort({A:1})            // use;

        .sort ({ A:1,B:1})         //use;

        .sort ( {A:-1,B:-1})        //use;

         .sort({A:-1,B:1})         // nonuse;

 

  Multikey Indexes多键索引:

  1。:

    document:  {A: [ 1,2,3]  }

      index:      {A:1}

    find ({A:1}) //如此使用该索引;

   2。复合索引:

    复合索引中只能出现一个多键索引,所以:

    index:{A:1,B:1}

    insert({ A:[1,2],B:[1,2]}) //error;

    insert( {A:[1,2] ,B: 1})  //pass;

   3.the array Field contain nested Objects:

    document:   {A: [ { a:1, b:2}, { a: 3, b: 4}]},

    index{ A.a : 1}  通过这样,对其建立索引;

   Text Index文本索引:

    通过正则来匹配文档,是很耗性能的。所以有这么一个text Index;

    使用: createIndex({A:‘text‘})   , 默认是使用英语。

    可以通过第二个属性来定义语言: createIndex({A:‘text‘}, { default_language:‘hans‘});

    hans是简体中文。可惜mongod企业版中才能使用中文的Text Index。

   Index Intersection索引交集:

    比如你有如下索引:

      index:{A:1},

      index:{B: 1,C:1},

      index { B:1}

    find (  { A:value , B :value } ) //支持

    find (  { A:value } ) .sort ( { B: 1})  //不支持

    find (  { B:value} ) .sort (   { C:1})  //支持,使用了 {B:1,C:1}这个索引,虽然有{B:1}这个single Index; 

  Index properties索引属性:

    1,TTL(设置document过期时间):

        db.test.createIndex( {expire:1} , { expireAfterSeconds:3000} ); 

        建议一个expire的索引,其在document中所接受的值必须为:BSON日期类型。或者Array<BSON>;

        插入的document将在其指定的时间+3000S后被删除。

        也就是插入时,就得到了这个过期时间。mongodb周期性地,将这个过期时间与目前时间比较,小于等于目前时间时,该数据过期。比如:

          db.test.insert ({ expire : new Date()}); 就会在3000S后过期。

        接下面的例子:

 

var time= new Date();
time.setFullYear(time.getFullYear()-1);

db.test.update({....},{expire:time});                      //将时间改为更早,使得document提前过期;
db.test.update({....},{$currentDate:{expire:true}})       //将时间修正为当前,强行续命!

        mongdb里的expire 与Redis中是不一样的。需要多理解。

    2,Unique唯一索引:

       create( { id:1} , {unique:ture} )

         在collection中每个document的id都是唯一,重复者不能写入。

    3,Partical局部索引

        create({ name:1}, { age:{$gt:20}});

         只对age大于20的document进行索引。与unique连用时,不被索引区不受unique限制。

    4,Sparse稀疏索引

        create({name:1},{sparse:true});

         不使用时,当有一个及以上document没有name属性时,db.test.distinct(‘name‘) 将会返回一个null。因为查询时,遍历了这些文档。

         使用稀疏,db.test.distinct(‘name‘)。不会查询没有name属性的document。返回应得值。

         db.find({}).sort({name:1})                          //默认不会使用稀疏索引。所以这里改用强制指定索引:

         db.find({}).sort({name:1}).hint({name:1})  //强行使用name Index;

                         db.find({}).hint({$natural:1})                       //禁用一切索引进行查询

 

 

 

 

 

 

在replSet上创建索引:

    https://docs.mongodb.com/manual/tutorial/build-indexes-on-replica-sets/

Mongodb笔记(二) Index

标签:efault   写入   text   get   选择   ted   大于   build   英语   

原文地址:http://www.cnblogs.com/ztwBlog/p/6281758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!