MongoDB创建索引

本文介绍了MongoDB中如何创建、查询、删除索引,包括复合索引和唯一索引的使用,以及处理索引中重复键的方法。通过示例展示了创建不同类型的索引,并提供了查看索引信息和删除索引的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建索引

 db.user.ensureIndex({telephone:1});

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

在MongoDB中,我们同样可以创建复合索引,如下:

db.user.ensureIndex({username: 1, age: -1});

该索引被创建后,基于username和age的查询将会用到该索引,或者是基于username的查询也会用到该索引,但是只是基于age的查询将不会用到该复合索引。因此可以说,如果想用到复合索引,必须在查询条件中包含复合索引中的前N个索引列。然而如果查询条件中的键值顺序和复合索引中的创建顺序不一致的话,MongoDB可以智能的帮助我们调整该顺序,以便使复合索引可以为查询所用。如:

db.user.find({"age": 30, "username": "stephen"})


对于上面示例中的查询条件,MongoDB在检索之前将会动态的调整查询条件文档的顺序,以使该查询可以用到刚刚创建的复合索引。

2、查询indexes

db.user.getIndexes();

[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "shoes.user"
        },
        {
                "v" : 1,
                "key" : {
                        "telephone" : 1
                },
                "name" : "telephone_1",
                "ns" : "shoes.user"
        }
]
 

3、创建唯一索引

在缺省情况下创建的索引均不是唯一索引。下面的示例将创建唯一索引,如:

db.user.ensureIndex({"userid":1},{"unique":true})


如果再次插入userid重复的文档时,MongoDB将报错,以提示插入重复键,如:

db.user.insert({"userid":5})
db.user.insert({"userid":5})

 

1

E11000 duplicate key error index: user.user.$userid_1 dup key: { : 5.0 }

如果插入的文档中不包含userid键,那么该文档中该键的值为null,如果多次插入类似的文档,MongoDB将会报出同样的错误,如:

 

db.user.insert({"userid1":5})

 

db.user.insert({"userid1":5})

 

1

E11000 duplicate key error index: user.user.$userid_1 dup key: { : null }

如果在创建唯一索引时已经存在了重复项,我们可以通过下面的命令帮助我们在创建唯一索引时消除重复文档,仅保留发现的第一个文档,如:

--先删除刚刚创建的唯一索引。

db.user.dropIndex({"userid":1})

--插入测试数据,以保证集合中有重复键存在。

db.test.remove()

db.test.insert({"userid":5})

db.test.insert({"userid":5})

--创建唯一索引,并消除重复数据。

db.user.ensureIndex({"userid":1},{"unique":true,"dropDups":true})

--查询结果确认,重复的键确实在创建索引时已经被删除。

db.user.find()

我们同样可以创建复合唯一索引,即保证复合键值唯一即可。如:

db.user.ensureIndex({"userid":1,"age":1},{"unique":true})

4、查看总索引记录大小

db.user.totalIndexSize();

5、读取当前集合的所有index信息

 db.user.reIndex()

{
        "nIndexesWas" : 2,
        "nIndexes" : 2,
        "indexes" : [
                {
                        "key" : {
                                "_id" : 1
                        },
                        "name" : "_id_",
                        "ns" : "cjq.user"
                },
                {
                        "key" : {
                                "telephone" : 1
                        },
                        "name" : "telephone_1",
                        "ns" : "cjq.user"
                }
        ],
        "ok" : 1
}


6、删除指定索引

 db.user.dropIndex({telephone:1});

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

7、删除所有索引索引

 db.user.dropIndexes();

{
        "nIndexesWas" : 2,
        "msg" : "non-_id indexes dropped for collection",
        "ok" : 1
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值