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?
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.
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.
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.
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 .
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("");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With