zoukankan      html  css  js  c++  java
  • 索引属性 name指定

    创建索引时的格式:
    db.collection.ensureIndex({param},{param})
    其中,第一个是索引的值,之前一直只用到了第一个,第二个参数便是索引的属性
    比较重要的属性有:
    名字
      db.collection.ensureIndex({},{name:''})
      在创建索引时,mongodb会自己给索引创建默认的名字,这种名字并不好记,我们看一下mongodb是怎么给自己命名的
    唯一性
    稀疏性
    是否定时删除:比如过期索引
    > db.suoyin.getIndexes()
    [
        {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1
            },
            "name" : "x_1",
            "ns" : "test.suoyin"
        }
    ]
    我们再增加一条索引y,排序方式为-1
    > db.suoyin.getIndexes()
    [
        {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1
            },
            "name" : "x_1",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "y" : -1
            },
            "name" : "y_-1",
            "ns" : "test.suoyin"
        }
    ]
    可以看到name的命名方式是key值+下划线+value值
    我们再创建一个复合索引
    > db.suoyin.ensureIndex({x:1,y:-1})
    再看索引
    > db.suoyin.getIndexes()
    [
        {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1
            },
            "name" : "x_1",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "y" : -1
            },
            "name" : "y_-1",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1,
                "y" : -1
            },
            "name" : "x_1_y_-1",
            "ns" : "test.suoyin"
        }
    ]
    可以看到,复合索引是多个key之前用下划线相隔,这种名字显得并不是很直观,如果再设置索引({x:1,y:1,z:1,m:1}),这样索引名字会特别长,不好记,另外一方面,mongodb对索引对名字有长度限制,现在对长度限制是125字节,为了避免长度超限,也避免获取索引时不能够很好的理解,所以我们可以自定义索引的名字
    > db.suoyin.ensureIndex({x:1,y:1,z:1,m:1},{name:'normal_index'})
    {
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 4,
        "numIndexesAfter" : 5,
        "ok" : 1
    }
    再次查看索引
    > db.suoyin.getIndexes()
    [
        {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1
            },
            "name" : "x_1",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "y" : -1
            },
            "name" : "y_-1",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1,
                "y" : -1
            },
            "name" : "x_1_y_-1",
            "ns" : "test.suoyin"
        },
        {
            "v" : 2,
            "key" : {
                "x" : 1,
                "y" : 1,
                "z" : 1,
                "m" : 1
            },
            "name" : "normal_index",
            "ns" : "test.suoyin"
        }
    ]
    可以看到最后一个名字已经指定为normal_index,这不仅让人容易理解,而且在删除索引时,也可以使用名字作为参数
    > db.suoyin.dropIndex('normal_index')
    { "nIndexesWas" : 5, "ok" : 1 }
    相比较指定x_1_y_-1_z_1,使用名字来代替是比较方便的一种方法
    > db.suoyin.getIndexes()
    [
        {
            "v" : 2,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "test.suoyin"
        }
    ]
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/wzndkj/p/9434518.html
Copyright © 2011-2022 走看看