zoukankan      html  css  js  c++  java
  • 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"
                }
        ]
        
  • 相关阅读:
    angular运行报错“Cannot find module 'ng2-translate'.”
    切换分支
    下载angular项目报错[ERROR] ionic-app-scripts has unexpectedly closed (exit code 1).
    通过原生SQL判断数据是否存在
    多图合并一张长图脚本
    科大讯飞--新冠肺炎检测赛道第八分享
    Mysql定时任务
    Mysql导出数据结构 or 数据
    G6Editor 边的参数配置
    百度坐标转腾讯坐标
  • 原文地址:https://www.cnblogs.com/zhoujun007/p/12388747.html
Copyright © 2011-2022 走看看