• 技术文章 >数据库 >MongoDB

    mongodb如何删除索引

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-19 09:13:22原创2945

    mongodb提供两种删除索引的方法:

    dropIndex()方法用于删除指定的索引;dropIndexes()方法用于删除全部的索引。

    例1:dropIndex()的用法

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    > db.users.dropIndex("name_1")

    { "nIndexesWas" : 5, "ok" : 1 }

    > db.users.dropIndex("name_1_age_1")

    { "nIndexesWas" : 4, "ok" : 1 }

    > db.users.getIndexSpecs()

    [

        {

            "v" : 1,

            "key" : {

                "_id" : 1

            },

            "name" : "_id_",

            "ns" : "test1.users"

        },

        {

            "v" : 1,

            "key" : {

                "name" : -1

            },

            "name" : "name_-1",

            "ns" : "test1.users"

        },

        {

            "v" : 1,

            "key" : {

                "age" : 1

            },

            "name" : "age_1",

            "ns" : "test1.users",

            "background" : 1

        }

    ]

    我们可以看到,name字段的索引和name与age字段的组合索引皆被删除。

    例2:dropIndexes()的用法

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    > db.users.dropIndexes()

    {

        "nIndexesWas" : 3,

        "msg" : "non-_id indexes dropped for collection",

        "ok" : 1

    }

    > db.users.getIndexSpecs()

    [

        {

            "v" : 1,

            "key" : {

                "_id" : 1

            },

            "name" : "_id_",

            "ns" : "test1.users"

        }

    ]

    在使用了dropIndexes()方法后,我们之前建的所有索引都被删除掉了。

    python学习网,大量的免费MongoDB入门教程,欢迎在线学习!

    专题推荐:mongodb 删除索引
    上一篇:如何把mongodb加入服务中 下一篇:mongodb端口占用怎么解决

    相关文章推荐

    • mongodb怎么建索引• mongodb有几种索引• 一文了解MongoDB索引• mongodb 如何查看索引

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网