

- 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 can I get a list of databases and collections on a MongoDB server?
To get a list of all databases, you need to use the below syntax −
use admin db.runCommand({listDatabases: 1});
To get a list of all collection names of a particular database, you need to use below syntax −
use yourDatabaseName; db.getCollectionNames();
Let us implement the above syntaxes −
Case 1 − To get a list of databases
> use admin switched to db admin > db.runCommand({listDatabases: 1});
This will produce the following output −
{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 1675264, "empty" : false }, { "name" : "config", "sizeOnDisk" : 131072, "empty" : false }, { "name" : "local", "sizeOnDisk" : 77824, "empty" : false }, { "name" : "main", "sizeOnDisk" : 184320, "empty" : false }, { "name" : "my", "sizeOnDisk" : 753664, "empty" : false }, { "name" : "sample", "sizeOnDisk" : 1286144, "empty" : false }, { "name" : "sampleDemo", "sizeOnDisk" : 352256, "empty" : false }, { "name" : "studentSearch", "sizeOnDisk" : 262144, "empty" : false }, { "name" : "test", "sizeOnDisk" : 15810560, "empty" : false }, { "name" : "university", "sizeOnDisk" : 229376, "empty" : false }, { "name" : "web", "sizeOnDisk" : 217088, "empty" : false } ], "totalSize" : 20979712, "ok" : 1 }
Case 2 − To get a list of collections
Here, we are getting the list of collections for database “sample”
> use sample; switched to db sample > db.getCollectionNames();
This will produce the following output −
[ "arraySizeErrorDemo", "basicInformationDemo", "copyThisCollectionToSampleDatabaseDemo", "documentWithAParticularFieldValueDemo", "employee", "findListOfIdsDemo", "findMimimumElementInArrayDemo", "findSubstring", "getAllRecordsFromSourceCollectionDemo", "getElementWithMaxIdDemo", "insertDocumentWithDateDemo", "internalArraySizeDemo", "largestDocumentDemo", "makingStudentInformationClone", "nestedArrayDemo", "oppositeAddToSetDemo", "prettyDemo", "returnOnlyUniqueValuesDemo", "selectItemDemo", "selectWhereInDemo", "sourceCollection", "specificFieldDemo", "studentInformation", "sumOfValueDemo", "sumTwoFieldsDemo", "truncateDemo", "updateFieldIfValueIsGreaterDemo", "updateInformation", "userInformation" ]
- Related Questions & Answers
- How can we display a list of currently existing MySQL databases on the server?
- Get a list of MySQL databases and version?
- How to get all the collections from all the MongoDB databases?
- Get MongoDB Databases in a JavaScript Array?
- How to get the list of all the MongoDB databases using java?
- How can I get elements from a Java List?
- How can I get a list of locally installed Python modules?
- How to list all collections from a particular MongoDB database?
- Is there a way to list collections in MongoDB?
- How to GRANT SELECT ON all tables in all databases on a server with MySQL?
- How do I display a list of objects based on a specific property with MongoDB?
- How can we get the list of MySQL server-side help categories?
- How to list all the Collections in a MongoDB database using Java?
- How can I start MySQL Server?
- How can I shutdown MySQL Server?
Advertisements