Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all collections in the MongoDB shell?

In the MongoDB shell, how do I list all collections for the current database that I'm using?

like image 585
coffee-grinder Avatar asked Jan 14 '12 22:01

coffee-grinder


People also ask

How do I show all collection data in MongoDB?

To get stats about MongoDB server, type the command db. stats() in MongoDB client. This will show the database name, number of collection and documents in the database.

Which command is used to check existing of collections in MongoDB?

If the database is not present, MongoDB will automatically create one. The collectionExists method can be used to check whether a collection is present or not: MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.

How do I get a list of databases in MongoDB?

There are different mongo shell list Databases commands in MongoDB. These commands can be run on the Mongo Shell to return the list of databases running on your MongoDB server. The show dbs Mongo Shell command returns the list of all databases running on MongoDB Server including default and user-defined databases.


1 Answers

You can do...

JavaScript (shell):

db.getCollectionNames() 

Node.js:

db.listCollections() 

Non-JavaScript (shell only):

show collections 

The reason I call that non-JavaScript is because:

$ mongo prodmongo/app --eval "show collections" MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell eval):1:5  $ mongo prodmongo/app --eval "db.getCollectionNames()" MongoDB shell version: 3.2.10 connecting to: prodmongo/app [     "Profiles",     "Unit_Info" ] 

If you really want that sweet, sweet show collections output, you can:

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')" MongoDB shell version: 3.2.10 connecting to: prodmongo/app Profiles Unit_Info 
like image 94
AdaTheDev Avatar answered Sep 19 '22 00:09

AdaTheDev