MongoDB笔记 索引(详细)

Posted Aurora1217

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB笔记 索引(详细)相关的知识,希望对你有一定的参考价值。

 

目录

1.单一字段索引

2.字段值唯一

3.多字段索引

4.其他索引方法


1.单一字段索引

命令:

db.collection_name.createIndex(<key>:<n>)
key:键名
n=1:表示升序
n=-1:表示降序

实例:

> db.books.insertMany(
... [
... name:"<a cat story>",price:20,color:"red",
... name:"<crying birds story>",price:20,color:"green",
... name:"<big dogs story>",price:25,color:"blue"
... ]
... )

        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("608bc108f85311b76bd55902"),
                ObjectId("608bc108f85311b76bd55903"),
                ObjectId("608bc108f85311b76bd55904")
        ]

> db.books.createIndex(name:1)

        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1

> db.books.find(name:$regex:/story>$/).pretty()

        "_id" : ObjectId("608bc108f85311b76bd55902"),
        "name" : "<a cat story>",
        "price" : 20,
        "color" : "red"


        "_id" : ObjectId("608bc108f85311b76bd55904"),
        "name" : "<big dogs story>",
        "price" : 25,
        "color" : "blue"


        "_id" : ObjectId("608bc108f85311b76bd55903"),
        "name" : "<crying birds story>",
        "price" : 20,
        "color" : "green"

2.字段值唯一

命令:

db.collection_name.createIndex(<key>:<n>,unique:true)

实例:

> db.books.createIndex(name:1,unique:true)

        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1

> db.books.insert(name:"<a cat story>",price:10,color:"pink")
WriteResult(
        "nInserted" : 0,
        "writeError" : 
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error collection: goodsdb.books index: name_1 dup key:  : \\"<a cat story>\\" "
        
)
原来有一个name为<a cat story>的文档
索引字段唯一之后,再插入一个name为<a cat story>将报错
> db.books.createIndex(price:1,unique:true)

        "ok" : 0,
        "errmsg" : "E11000 duplicate key error collection: goodsdb.books index: price_1 dup key:  : 20.0 ",
        "code" : 11000,
        "codeName" : "DuplicateKey"

当price键对应的值不唯一(有两个price键对应的值都是20)时,设置唯一索引不成功

3.多字段索引

命令:

db.collection_name.createIndex(<key>:<n>,<key>:<n>,…)

实例:

> db.books.createIndex(price:1,color:-1)

        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1

> db.books.find().sort(price:-1,color:1).pretty()

        "_id" : ObjectId("608bc487f85311b76bd5590d"),
        "name" : "<big dogs story>",
        "price" : 25,
        "color" : "blue"


        "_id" : ObjectId("608bc487f85311b76bd5590c"),
        "name" : "<crying birds story>",
        "price" : 20,
        "color" : "green"


        "_id" : ObjectId("608bc487f85311b76bd5590b"),
        "name" : "<a cat story>",
        "price" : 20,
        "color" : "red"

4.其他索引方法

(1)返回所有索引

命令:

db.collection.getIndexes()

(2)移除集合制定索引

命令:

db.collection.dropIndex(name:1)


(3)移除全部索引

命令:

db.collection.dropIndex()

实例:

> db.books.getIndexes()
[
        
                "v" : 2,
                "key" : 
                        "_id" : 1
                ,
                "name" : "_id_",
                "ns" : "goodsdb.books"
        ,
        
                "v" : 2,
                "key" : 
                        "name" : 1
                ,
                "name" : "name_1",
                "ns" : "goodsdb.books"
        ,
        
                "v" : 2,
                "key" : 
                        "price" : 1,
                        "color" : -1
                ,
                "name" : "price_1_color_-1",
                "ns" : "goodsdb.books"
        
]
> db.books.dropIndex(name:1)
 "nIndexesWas" : 3, "ok" : 1 
> db.books.getIndexes()
[
        
                "v" : 2,
                "key" : 
                        "_id" : 1
                ,
                "name" : "_id_",
                "ns" : "goodsdb.books"
        ,
        
                "v" : 2,
                "key" : 
                        "price" : 1,
                        "color" : -1
                ,
                "name" : "price_1_color_-1",
                "ns" : "goodsdb.books"
        
]

下一篇:MongoDB笔记(八) 聚合(详细)

 

以上是关于MongoDB笔记 索引(详细)的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB 学习笔记之 索引

MongoDb索引--基础篇

MongoDB升序/降序定位错误值

MongoDB创建索引

SQL Server 索引 - 升序或降序,有啥区别?

python:argsort,将数组升序或降序,将矩阵每一行升序或降序,返回其索引