Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Web Technology by (47.6k points)

Is it possible to show all collections and its contents in MongoDB?

Is the only way to show one by one?

2 Answers

0 votes
by (106k points)

To show all contents from all collections in MongoDB you can use the below-mentioned code:-

var collections = db.getCollectionNames(); 

for(var i = 0; i< collections.length; i++){ 

print('Collection: ' + collections[i]); db.getCollection(collections[i]).find().forEach(printjson)

}

This script might get what you want. It prints the name of each collection and then prints its elements in json. 

0 votes
by (107k points)

Once you are in the terminal/command line, access the database/collection you want to use as follows:

show dbs

use <db name>

show collections

After that choose your collection and type the following command to see all the contents of that collection:

db.collectionName.find()

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...