

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to operate on all databases from the MongoDB shell?
To operate on all databases from MongoDB shell, you can use listDatabases along with adminCommand().
Let’s say we are using a sample database “test”. At first, check the current database with the help of db command.
Following is the query to get the current database
> db;
This will produce the following output
Test
Following is the query to operate on all the databases from the Mongo shell
> var allDatabaseList = db.adminCommand('listDatabases');
Now you need to use printjson() in order to print all databases. Following is the query
> printjson (allDatabaseList);
This will produce the following output
{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 495616, "empty" : false }, { "name" : "config", "sizeOnDisk" : 98304, "empty" : false }, { "name" : "local", "sizeOnDisk" : 73728, "empty" : false }, { "name" : "sample", "sizeOnDisk" : 1388544, "empty" : false }, { "name" : "sampleDemo", "sizeOnDisk" : 278528, "empty" : false }, { "name" : "studentSearch", "sizeOnDisk" : 262144, "empty" : false }, { "name" : "test", "sizeOnDisk" : 9695232, "empty" : false } ], "totalSize" : 12292096, "ok" : 1 }
- Related Questions & Answers
- How to list all databases in the Mongo shell?
- How to get all the collections from all the MongoDB databases?
- MongoDB find() to operate on recursive search?
- How to iterate over all MongoDB databases?
- How to get the list of all the MongoDB databases using java?
- How to define aliases in the MongoDB Shell?
- Use result from MongoDB in shell script?
- How to GRANT SELECT ON all tables in all databases on a server with MySQL?
- Deleting all records of a collection in MongoDB Shell?
- How to list all collections in the Mongo shell?
- How to list all users in the Mongo shell?
- How to terminate a MongoDB shell script earlier?
- How to print document value in MongoDB shell?
- How to unset a variable in MongoDB shell?
- How to run MongoDB shell using mongos command?
Advertisements