In the MongoDB shell, how do I list all collections for the current database that I'm using?
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.
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.
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.
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
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