Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (5k points)

I want to list all collections of my current database which is being used by me. How to do it?

1 Answer

0 votes
by (10.2k points)

Try this out:

JavaScript (shell):

db.getCollectionNames()

For Node.js:

db.listCollections()

Non-JavaScript (shell only):

show collections

Why this is called as 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 want to show collections output:

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"

MongoDB shell version: 3.2.10

connecting to: prodmongo/app

Profiles

Unit_Info

 If you want to learn MongoDB, checkout this MongoDB tutorial from Intellipaat.

Related questions

0 votes
2 answers
asked Sep 4, 2019 in SQL by Sammy (47.6k points)
0 votes
2 answers
0 votes
2 answers
0 votes
2 answers

Browse Categories

...