2010-05-07 49 views

回答

137

从贝:

db.test.getIndexes() 

对于外壳的帮助,你应该尝试:

help; 
db.help(); 
db.test.help(); 
13

如果你想在你的数据库中所有索引的列表:

use "yourdbname" 

db.system.indexes.find() 
5

您也可以输出所有与他们的大小指数一起:

db.collectionName.stats().indexSizes 

此外,检查db.collectionName.stats()给你很多像paddingFactor这样有趣的信息,集合的大小和其内部元素的数量。

4

如果要列出所有索引:

db.getCollectionNames().forEach(function(collection) { 
    indexes = db[collection].getIndexes(); 
    print("Indexes for " + collection + ":"); 
    printjson(indexes); 
}); 
2

考虑这一步,如果你想找到的所有集合的所有索引,这个脚本(由胡安·卡洛斯·法拉的剧本here修改)给出你的一些有用的输出,包括索引细节的JSON打印输出:

// Switch to admin database and get list of databases. 
db = db.getSiblingDB("admin"); 
dbs = db.runCommand({ "listDatabases": 1}).databases; 


// Iterate through each database and get its collections. 
dbs.forEach(function(database) { 
db = db.getSiblingDB(database.name); 
cols = db.getCollectionNames(); 

// Iterate through each collection. 
cols.forEach(function(col) { 

    //Find all indexes for each collection 
    indexes = db[col].getIndexes(); 

    indexes.forEach(function(idx) { 
     print("Database:" + database.name + " | Collection:" +col+ " | Index:" + idx.name); 
     printjson(indexes); 
     }); 


    }); 

}); 
+0

这真的很有帮助,但我认为'printjson(indexes);'应该是'printjson(id x)的;' – 2018-03-09 22:56:40