Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MongoDB, how do I print all collection counts and indexes?

Tags:

mongodb

I need to verify data before migrating to another server and I want to make sure all documents and indexes and transferred properly. Is there a command and I run to do this?

like image 515
Zen Momentum Avatar asked Sep 18 '15 01:09

Zen Momentum


People also ask

How do I see all indexes in MongoDB?

Finding indexes You can find all the available indexes in a MongoDB collection by using the getIndexes method. This will return all the indexes in a specific collection. Result: The output contains the default _id index and the user-created index student name index.

How do I show all collections in MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.

Where is total count in MongoDB?

count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.

Which of the following command is used to get all the indexes on a collection?

List All Indexes on a Collection. To return a list of all indexes on a collection, use the db. collection. getIndexes() method or a similar method for your driver .


1 Answers

This script will output what you want:

db = db.getSiblingDB('admin');

var dbs = db.adminCommand('listDatabases');

dbs.databases.forEach(function(database){
  print("Database: " + database.name);
  print("-----");

  db = db.getSiblingDB(database.name);

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

  print("");

});
like image 170
David H. Bennett Avatar answered Sep 28 '22 04:09

David H. Bennett