美文网首页
mongodb索引操作

mongodb索引操作

作者: simplerandom | 来源:发表于2020-06-15 16:47 被阅读0次

    查看索引

    > db.myjihe.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "mydb.myjihe"
            }
    ]
    

    创建复合索引:"0"字段升序“p”降序

    > db.myjihe.createIndex({"0":1,"p":-1})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    > db.myjihe.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "mydb.myjihe"
            },
            {
                    "v" : 2,
                    "key" : {
                            "0" : 1,
                            "p" : -1
                    },
                    "name" : "0_1_p_-1",
                    "ns" : "mydb.myjihe"
            }
    ]
    

    删除索引

    > db.myjihe.dropIndex({"0":1,"p":-1})
    { "nIndexesWas" : 2, "ok" : 1 }
    > db.myjihe.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "mydb.myjihe"
            }
    ]
    

    相关文章

      网友评论

          本文标题:mongodb索引操作

          本文链接:https://www.haomeiwen.com/subject/fqrhxktx.html