mongoDB_08索引的操作

索引-Index

索引支持在MongoDB中高效的执行查询。如果没有索引,MongoDB必须执行全部扫描,即扫描集合中的每一个文档,以选择与查询语句匹配的文档,

如果查询存在适当的索引,MongoDB可以用该索引限制必须检查的文档数。


索引是特殊的数据结构,它以易于遍历的形式存储集合数数据集的一小部分。索引存储特定字段或一组字段的值,按字段值排序。索引项的排序支持有效的相等匹配和基于范围的查询操作。

MongoDB索引使用B树数据结构.(确切的说是B-Tree,Mysql使B+Tree)

  • 索引的类型

    单字段索引

    MongoDB支持在文档的单个字段上创建用户定义的升序/降序索引,称为单字段索引(Single Field Index)。
    对于单个字段的索引和排序操作,索引键的排序顺序(升序/降序)并不重要,因为MongoDB可以在任何方向上遍历索引。
    
    

    复合索引

    MongoDB支持多个字段的用户定义索引,即复合索引(Compound Index)
    复合索引中列出的字段顺序具有重要的意义。(eg:如果复合索引由{userid:1,score:-1}组成,则索引首先按照userid正序排序,然后在每个userid的值内,再按照score倒序排序)
    

    其它索引

    地理空间索引(Geospatial Index)
    文本索引(Text Indexes)
    哈希索引(Hashed  Indexes)
    
  • 索引的操作

    • 1:索引的查看
    1:索引的查看
    返回一个集合中所有索引的数组.
    语法:
        db.collection.getIndexes()
    ---------------------------------------------------------------------
    例子:
        查看comment集合中所有的索引情况
        db.comment.getIndexes()
    
    执行:
    > db.comment.getIndexes()
    [
            {
                    "v" : 2,                       #代表索引引擎的版本号是2
                    "key" : {
                            "_id" : 1			  #索引被默认加在_id字段,升序方式
                    },
                    "name" : "_id_",               #索引的名称字段名+_
                    "ns" : "articledb.comment"     #存放的命名空间
            }
    ]
    
    • 2:索引的创建

      在集合上创建索引.
      语法:
          db.collection.createIndex(keys,options)
          
      1:单字段索引例子:对userid字段建立索引
      	db.comment.createIndex({userid:1})
      执行:
      > db.comment.createIndex({userid:1})
      {
              "createdCollectionAutomatically" : false,
              "numIndexesBefore" : 1,
              "numIndexesAfter" : 2,
              "ok" : 1
      }
      
      #查看索引是否创建
      > db.comment.getIndexes()
      [
              {
                      "v" : 2,
                      "key" : {
                              "_id" : 1
                      },
                      "name" : "_id_",
                      "ns" : "comment.comment"
              },
              {
                      "v" : 2,
                      "key" : {
                              "userid" : 1
                      },
                      "name" : "userid_1",
                      "ns" : "comment.comment"
              }
      ]
      
      2:复合索引:对userid和nickname同时建立复合(Compound)索引
          db.comment.createIndex({userid:1,nickname:-1})
          
      > db.comment.createIndex({userid:1,nickname:-1})     #:1升序  -1:降序
      {
              "createdCollectionAutomatically" : false,
              "numIndexesBefore" : 2,
              "numIndexesAfter" : 3,
              "ok" : 1
      }
      
      #查看索引
      > db.comment.getIndexes()
      [
              {
                      "v" : 2,
                      "key" : {
                              "_id" : 1
                      },
                      "name" : "_id_",
                      "ns" : "comment.comment"
              },
              {
                      "v" : 2,
                      "key" : {
                              "userid" : 1
                      },
                      "name" : "userid_1",
                      "ns" : "comment.comment"
              },
              {
                      "v" : 2,
                      "key" : {
                              "userid" : 1,
                              "nickname" : -1
                      },
                      "name" : "userid_1_nickname_-1",
                      "ns" : "comment.comment"
              }
      ]
      
    • 索引的移除

      可以移除指定的索引或者移除所有索引
      1:移除指定的索引
          db.comment.dropIndex(index)
      例子:
          删除comment集合中userid字段上的升序索引
          db.comment.dropIndex({userid:1})
          
      执行:
      > db.comment.dropIndex({userid:1})
      { "nIndexesWas" : 3, "ok" : 1 }
      
      
      2:删除所有索引----  _id_ 索引不会被删除
      db.comment.dropIndexes()
      执行:
      > db.comment.dropIndexes()
      {
              "nIndexesWas" : 2,
              "msg" : "non-_id indexes dropped for collection",
              "ok" : 1
      }
      > db.comment.getIndexes()                  #默认索引不会被删除
      [
              {
                      "v" : 2,
                      "key" : {
                              "_id" : 1
                      },
                      "name" : "_id_",
                      "ns" : "comment.comment"
              }
      ]
      
posted @ 2020-03-01 11:19  Noob52037  阅读(98)  评论(0编辑  收藏  举报