MongoDB Show all products from all collections.

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

Is this the only way to show one by one?

+115
collections find mongodb
Jul 27 '14 at 21:07
source share
7 answers

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

show dbs use <db name> show collections 

select your collection and enter the following to view all the contents of this collection:

 db.collectionName.find() 

Read more here in the MongoDB Quick Reference Guide .

+201
Apr 21 '15 at 16:54
source share

Step 1: Browse all your databases:

 show dbs 

Step 2: Select a Database

 use your_database_name 

Step 3: Show Collections

 show collections 

This will list all the collections in the database of your choice.

Step 4: View All Data

 db.collection_name.find() 

or

 db.collection_name.find().pretty() 
+86
Mar 23 '18 at 12:47
source share
 var collections = db.getCollectionNames(); for(var i = 0; i< collections.length; i++){ print('Collection: ' + collections[i]); // print the name of each collection db.getCollection(collections[i]).find().forEach(printjson); //and then print the json of each of its elements } 

I think this script can get what you want. It prints the name of each collection, and then prints its elements in json.

+29
Jul 28 '14 at 2:31
source share

Before writing below, first log into your cmd or PowerShell

 TYPE: mongo //To get into MongoDB shell use <Your_dbName> //For Creating or making use of existing db 

To list all collection names, use any of the following options: -

 show collections //output every collection OR show tables OR db.getCollectionNames() //shows all collections as a list 

To display the entire contents of the collections or data, use the code below that was sent by Bruno_Ferreira.

 var collections = db.getCollectionNames(); for(var i = 0; i< collections.length; i++) { print('Collection: ' + collections[i]); // print the name of each collection db.getCollection(collections[i]).find().forEach(printjson); //and then print the json of each of its elements } 
+5
Jun 06 '16 at 10:26
source share

Here:

 db.collection_name.find().toArray().then(...function...) 
+2
Dec 18 '18 at 15:15
source share

This will do:

 db.getCollectionNames().forEach(c => { db[c].find().forEach(d => { print(c); printjson(d) }) }) 
+1
Apr 09 '19 at 14:46
source share

I prefer a different approach if you use the mongo shell:

Others will answer first: use my_database_name then:

 db.getCollectionNames().map( (name) => ({[name]: db[name].find().toArray().length}) ) 

This query will show you something like this:

 [ { "agreements" : 60 }, { "libraries" : 45 }, { "templates" : 9 }, { "users" : 18 } ] 

You can use a similar approach with db.getCollectionInfos() this is very useful if you have so much data.

0
Jul 24 '19 at 6:29
source share



All Articles