为了一次建立多个索引,您需要使用createIndexes()
多个键并将其传递到数组中。以下是一次建立多个索引的查询。
>db.multipleIndexesDemo.createIndexes([{"First":1},{"Second":1},{"Third":1},{"Fourth":1},{"Fifth":1}]);
这将产生以下输出
{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 6, "ok" : 1 }
现在获取所有索引
> db.multipleIndexesDemo.getIndexes();
这将产生以下输出
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.multipleIndexesDemo" }, { "v" : 2, "key" : { "First" : 1 }, "name" : "First_1", "ns" : "test.multipleIndexesDemo" }, { "v" : 2, "key" : { "Second" : 1 }, "name" : "Second_1", "ns" : "test.multipleIndexesDemo" }, { "v" : 2, "key" : { "Third" : 1 }, "name" : "Third_1", "ns" : "test.multipleIndexesDemo" }, { "v" : 2, "key" : { "Fourth" : 1 }, "name" : "Fourth_1", "ns" : "test.multipleIndexesDemo" }, { "v" : 2, "key" : { "Fifth" : 1 }, "name" : "Fifth_1", "ns" : "test.multipleIndexesDemo" } ]