Lista indeksów w MongoDB?

Czy jest sposób, aby zobaczyć listę indeksów na kolekcji w mongodb w powłoce? przeczytałem http://www.mongodb.org/display/DOCS/Indexes but I dont see anything

Author: Community, 2010-05-07

6 answers

Z powłoki:

db.test.getIndexes()

Aby uzyskać pomoc shell należy spróbować:

help;
db.help();
db.test.help();
 137
Author: mdirolf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-07 22:52:22

A jeśli chcesz uzyskać listę wszystkich indeksów w swojej bazie danych:

use "yourdbname"

db.system.indexes.find()
 13
Author: Marian Zagoruiko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-20 21:59:19

Upewnij się, że używasz swojej kolekcji:

db.collection.getIndexes()

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

 8
Author: Kamilski81,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-13 12:43:49

Możesz również wypisać wszystkie indeksy wraz z ich rozmiarem:

db.collectionName.stats().indexSizes

Sprawdź również, czy db.collectionName.stats() daje Ci wiele ciekawych informacji, takich jak paddingFactor, rozmiar kolekcji i ilość elementów w niej zawartych.

 5
Author: Salvador Dali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-31 05:41:19

Jeśli chcesz wyświetlić listę wszystkich indeksów:

db.getCollectionNames().forEach(function(collection) {
   indexes = db[collection].getIndexes();
   print("Indexes for " + collection + ":");
   printjson(indexes);
});
 5
Author: Somnath Muluk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-26 15:26:38

Idąc o krok dalej, jeśli chcesz znaleźć wszystkie indeksy we wszystkich kolekcjach, ten skrypt (zmodyfikowany na podstawie skryptu Juana Carlosa Faraha Tutaj) daje Ci kilka przydatnych wyników, w tym Wydruk JSON szczegółów indeksu:

 // 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);
         });


    });

});
 2
Author: DCaugs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:28