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
            }
    ]

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

  • 相关阅读:
    js获取对象的最后一个
    vue UI框架
    从前端和后端两个角度分析jsonp跨域访问(完整实例)
    Ajax跨域访问解决方案
    js中将字符串转换成json的三种方式
    【转载】COM 组件设计与应用(九)——IDispatch 接口 for VC6.0
    【转载】COM 组件设计与应用(八)——实现多接口
    【转载】COM 组件设计与应用(七)——编译、注册、调用
    【转载】COM 组件设计与应用(六)——用 ATL 写第一个组件
    【转载】COM 组件设计与应用(五)——用 ATL 写第一个组件
  • 原文地址:https://www.cnblogs.com/libingql/p/2080658.html
Copyright © 2011-2022 走看看