
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 Articles
- How to get all the collections from all the MongoDB databases?
- How to list all databases in the Mongo shell?
- 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 GRANT SELECT ON all tables in all databases on a server with MySQL?
- How to define aliases in the MongoDB Shell?
- Use result from MongoDB in shell script?
- Deleting all records of a collection in MongoDB Shell?
- How to get items with a specific value from documents using MongoDB 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 list all collections in the Mongo shell?
- How to list all users in the Mongo shell?

Advertisements