zoukankan      html  css  js  c++  java
  • MongoDB聚集索引基本操作

    查看当前聚集的全部索引

    > db.Account.getIndexes()
    [
            {
                    
    "name" : "_id_",
                    
    "ns" : "ChatRoom.Account",
                    
    "key" : {
                            
    "_id" : 1
                    },
                    
    "v" : 0
            }
    ]

     创建单列索引

    > db.Account.ensureIndex({"UserName":1})

    --1:ASC   -1:DESC

    创建单列唯一索引

    > db.Account.ensureIndex({"UserName" : 1},{"unique" : true})

    创建单列唯一不重复索引

    > db.Account.ensureIndex({"UserName" : 1},{"unique" : true,"dropDups" : true})

    创建组合索引

    > db.Account.ensureIndex({"UserName":1,"Email":1})
    > db.Account.getIndexes()
    [
            {
                    
    "name" : "_id_",
                    
    "ns" : "ChatRoom.Account",
                    
    "key" : {
                            
    "_id" : 1
                    },
                    
    "v" : 0
            },
            {
                    
    "_id" : ObjectId("4df7092607444568af61cffd"),
                    
    "ns" : "ChatRoom.Account",
                    
    "key" : {
                            
    "UserName" : 1
                    },
                    
    "name" : "UserName_1",
                    
    "v" : 0
            },
            {
                    
    "_id" : ObjectId("4df70dae07444568af61cffe"),
                    
    "ns" : "ChatRoom.Account",
                    
    "key" : {
                            
    "UserName" : 1,
                            
    "Email" : 1
                    },
                    
    "name" : "UserName_1_Email_1",
                    
    "v" : 0
            }
    ]

    删除索引

    > db.Account.dropIndex({"UserName":1})
    "nIndexesWas" : 3"ok" : 1 }
    > db.Account.dropIndex("UserName_1")
    "nIndexesWas" : 3"ok" : 1 }

    删除聚集全部的索引

    > db.Account.dropIndexes()
    {
            
    "nIndexesWas" : 3,
            
    "msg" : "non-_id indexes dropped for collection",
            
    "ok" : 1
    }
    > db.Account.getIndexes()
    [
            {
                    
    "name" : "_id_",
                    
    "ns" : "ChatRoom.Account",
                    
    "key" : {
                            
    "_id" : 1
                    },
                    
    "v" : 0
            }
    ]

    --删除全部索引不会删除默认索引

  • 相关阅读:
    青春如同奔流的江河,一去不回来不及道别
    关于RESOURCE_SEMAPHORE等待类型
    sql server 2008 The fulltext filter daemon host (FDHost) process has stopped abnormally.
    [转]SQL Server 2008 R2 Pricing
    64bit sql server 2008 sp1 使用lock pages in memory 具体操作
    [转]Fun with Locked Pages, AWE, Task Manager, and the Working Set…
    关掉和开启win7的Windows Search服务
    How to recreate the msdb database in SQL Server 2005
    Kimberly L. Tripp的sp_helpindex2 for sql server 2005/2008,并修复了for sqk2k8的一个bug
    表无索引为什么sp_spaceused 中的index_size不为0
  • 原文地址:https://www.cnblogs.com/libingql/p/2080658.html
Copyright © 2011-2022 走看看