有没有办法查看Shell MongoDB中收藏集中的索引列表?我读了 http://www.mongodb.org/display/docs/indexes 但是我什么都没看见

有帮助吗?

解决方案

来自外壳:

db.test.getIndexes()

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

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

其他提示

如果您想获取数据库中所有索引的列表:

use "yourdbname"

db.system.indexes.find()

如果要列出所有索引:

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

确保您使用您的收藏:

db.collection.getIndexes()

http://docs.mongodb.org/manual/administration/indexes/#information-about-indexes

您还可以将所有索引与它们的大小一起输出:

db.collectionName.stats().indexSizes

还要检查一下 db.collectionName.stats() 为您提供许多有趣的信息,例如PaddingFactor,集合的大小和其中的元素数量。

更进一步,如果您想在所有集合上找到所有索引,则此脚本(根据Juan Carlos Farah的脚本进行了修改 这里)为您提供一些有用的输出,包括索引详细信息的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);
         });


    });

});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top