zoukankan      html  css  js  c++  java
  • MongoDB索引

    MongoDB索引

    查看索引

    1 .查看一个集合有哪些索引

    > db.table.getIndexKeys()
            
    > db.c2.getIndexKeys()
    [ { "_id" : 1 } ]
    

    2 .索引详情

    > db.c2.getIndexes()
    

    3 .查看集合的相关信息 【索引、大小等】

    > db.c2.stats()
    

    创建索引

    普通索引语法:

    > db.table.ensureIndex({key:1})
    

    1 .将c2 集合的 name 字段添加索引

    > db.c2.ensureIndex({name:1})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    

    创建唯一索引语法:

        > db.table.ensureIndex({key:1},{unique:1})
    

    1 .将name字段创建唯一索引 unique:1

    > db.c2.ensureIndex({name:1},{unique:1})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    

    2 .查看唯一索引 "unique" : true

    > db.c2.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "c2.c2"
            },
            {
                    "v" : 2,
                    "unique" : true,
                    "key" : {
                            "name" : 1
                    },
                    "name" : "name_1",
                    "ns" : "c2.c2"
            }
    ]
    

    删除索引

    > db.c2.dropIndex({name:1})
    { "nIndexesWas" : 2, "ok" : 1 }
    
  • 相关阅读:
    bzoj 1856 组合
    bzoj 2809 左偏树平衡树启发式合并
    【HMOI】小C的填数游戏 DP+线段树维护
    【HNOI】 小A的树 tree-dp
    bzoj 1483 链表启发式合并
    bzoj 2733 平衡树启发式合并
    bzoj 2669 状压DP
    bzoj 2165 DP
    【HNOI】 lct tree-dp
    远程debug配置
  • 原文地址:https://www.cnblogs.com/baolin2200/p/10070872.html
Copyright © 2011-2022 走看看