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笔记(八) 聚合(详细)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值