管理索引
- Mongo Shell
在本页面
此页显示如何管理现有索引。有关创建索引的说明,请参阅特定索引类型页。
以下部分提供了查看集合或整个数据库上现有索引的方法。
例如,要查看
people
集合上的所有索引,运行以下命令:db.people.getIndexes()
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});
从3.0版本开始,MongoDB不再支持对系统的直接访问。索引集合,以前用于列出数据库中的所有索引。
// The following finds all hashed indexes
db.adminCommand("listDatabases").databases.forEach(function(d){
let mdb = db.getSiblingDB(d.name);
mdb.getCollectionInfos({ type: "collection" }).forEach(function(c){
let currentCollection = mdb.getCollection(c.name);
currentCollection.getIndexes().forEach(function(idx){
let idxValues = Object.values(Object.assign({}, idx.key));
if (idxValues.includes("hashed")) {
print("Hashed index: " + idx.name + " on " + idx.ns);
printjson(idx);
};
});
});
});
MongoDB提供了两种方法从集合中删除索引:
例如,下面的操作删除了 accounts 集合中的 tax-id 字段的升序索引:
db.accounts.dropIndex( { "tax-id": 1 } )
该操作返回一个文档,其中显示了该操作的状态:
{ "nIndexesWas" : 3, "ok" : 1 }
其中
nIndexesWas
的值反映了_在删除这个索引之前_的索引数量。注意
例如,下面的命令从 accounts 集合中删除所有索引:
db.accounts.dropIndexes()
如果分片集合在每个包含该分片块的分片上没有完全相同的索引(包括索引选项),则该集合具有不一致的索引。虽然在正常操作中不应该出现索引不一致的情况,但也会出现索引不一致的情况,例如:
- 当用户创建具有
unique
键约束的索引并且一个分片包含具有重复文档的块时。在这种情况下,创建索引操作可能会在没有重复的分片上成功,但在没有重复的分片上不会成功。
从MongoDB 4.2.6,配置服务器主,默认情况下,检查索引不一致在分片的碎片集合,和命令("serverStatus"),主要配置服务器上运行时,返回字段
shardedIndexConsistency
来报告索引不一致的分片集合的数量。注意下面的管道用于MongoDB 4.2.4及以上版本。
- 1.const pipeline = [// Get indexes and the shards that they belong to.{$indexStats: {}},// Attach a list of all shards which reported indexes to each document from $indexStats.{$group: {_id: null, indexDoc: {$push: "$$ROOT"}, allShards: {$addToSet: "$shard"}}},// Unwind the generated array back into an array of index documents.{$unwind: "$indexDoc"},// Group by index name.{$group: {"_id": "$indexDoc.name","shards": {$push: "$indexDoc.shard"},// Convert each index specification into an array of its properties// that can be compared using set operators."specs": {$push: {$objectToArray: {$ifNull: ["$indexDoc.spec", {}]}}},"allShards": {$first: "$allShards"}}},// Compute which indexes are not present on all targeted shards and// which index specification properties aren't the same across all shards.{$project: {missingFromShards: {$setDifference: ["$allShards", "$shards"]},inconsistentProperties: {$setDifference: [{$reduce: {input: "$specs",initialValue: {$arrayElemAt: ["$specs", 0]},in: {$setUnion: ["$$value", "$$this"]}}},{$reduce: {input: "$specs",initialValue: {$arrayElemAt: ["$specs", 0]},in: {$setIntersection: ["$$value", "$$this"]}}}]}}},// Only return output that indicates an index was inconsistent, i.e. either a shard was missing// an index or a property on at least one shard was not the same on all others.{$match: {$expr:{$or: [{$gt: [{$size: "$missingFromShards"}, 0]},{$gt: [{$size: "$inconsistentProperties"}, 0]},]}}},// Output relevant fields.{$project: {_id: 0, indexName: "$$ROOT._id", inconsistentProperties: 1, missingFromShards: 1}}];
- 2.运行要测试的分片集合的聚合管道。例如,要测试分片集合是否测试。在相关的碎片上有不一致的索引:db.getSiblingDB("test").reviews.aggregate(pipeline)如果集合的索引不一致,则该集合的聚合将返回关于不一致索引的详细信息:{ "missingFromShards" : [ "shardB" ], "inconsistentProperties" : [ ], "indexName" : "page_1_score_1" }{ "missingFromShards" : [ ], "inconsistentProperties" : [ { "k" : "expireAfterSeconds", "v" : 60 }, { "k" : "expireAfterSeconds", "v" : 600 } ], "indexName" : "reviewDt_1" }
返回的文档指出了分片集合 test.reviews 的两个不一致之处:
- 1.shardB上的集合中缺少一个名为
page_1_score_1
的索引。- 1.一个名为
reviewDt_1
的索引在集合的各个分片上具有不一致的属性,特别是expireAfterSeconds属性不同。
要解决特定分片集合中缺少索引的不一致问题
从受影响的分片上的集合中删除不正确的索引,然后重建索引。要重建索引,您可以:
- 或者
要解决索引属性在各个分片之间的差异
从受影响的分片上的集合中删除不正确的索引,并重新构建索引。重建索引,你可以:
- 或者