本文介绍了一次构建多个索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在大型 MongoDB 集合中构建五个索引.我熟悉 ensureIndex 操作,但我不知道使用单个命令创建所有五个索引的方法.在 MongoDB 中可以批量创建索引吗?
I have a need to build five indexes in a large MongoDB collection. I'm familiar with the ensureIndex operation, but I do not know of a way to create all five of the indexes with a single command. Is this batch index creation possible in MongoDB?
推荐答案
这在 shell 中非常简单,createIndexes
的集合有一个扩展,你只需传入你想要的键在其上创建索引.
This is pretty simple within the shell, there is a extention to the collection of createIndexes
and you just pass in the keys you wish to create indexes on.
db.test.createIndexes([
{ "a" : 1 },
{ "b" : 1 },
{ "c" : 1 },
{ "d" : 1 },
{ "e" : 1 }
]);
这会给我们以下内容
> db.test.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.test"
},
{
"v" : 2,
"key" : {
"a" : 1
},
"name" : "a_1",
"ns" : "test.test"
},
{
"v" : 2,
"key" : {
"b" : 1
},
"name" : "b_1",
"ns" : "test.test"
},
{
"v" : 2,
"key" : {
"c" : 1
},
"name" : "c_1",
"ns" : "test.test"
},
{
"v" : 2,
"key" : {
"d" : 1
},
"name" : "d_1",
"ns" : "test.test"
},
{
"v" : 2,
"key" : {
"e" : 1
},
"name" : "e_1",
"ns" : "test.test"
}
]
>
这篇关于一次构建多个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!