259

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

Is the only way to show one by one?

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Reno
  • 2,639
  • 2
  • 12
  • 7

7 Answers7

406

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

show dbs
use <db name>
show collections

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

db.collectionName.find()

More info here on the MongoDB Quick Reference Guide.

sharkySharks
  • 4,254
  • 1
  • 10
  • 7
  • 1
    Please state this as the correct answer. You can only view ALL content from ALL collections by writing code, not querying via cli – imbatman May 17 '17 at 21:33
  • 41
    If you need to visually tidy up the collection presented to you, I also recommend: `db.collectionName.find().pretty()` – Steven Ventimiglia Nov 09 '17 at 20:21
  • 13
    Keep in mind this doesn't work if you have certain characters (like a hyphen) in the collection name. In that case use `db["collection-name"].find() ` – t-bone May 03 '19 at 00:51
  • Thanks @Bossan for the clarification. Helped a lot. – Bilaal Abdel Hassan Feb 03 '22 at 05:06
  • @imbatman, please **don't** mark this as the correct answer. And you can drop the "please", for against facts there's no arguing. Check Juan de Dios' [answer](https://stackoverflow.com/a/57176568/2571805), yonzen's [answer](https://stackoverflow.com/a/55595557/2571805), or [mine](https://stackoverflow.com/a/76953032/2571805). All these do something you're endorsing against: answering the question. – Ricardo Aug 22 '23 at 11:56
44
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 might get what you want. It prints the name of each collection and then prints its elements in json.

Bruno_Ferreira
  • 1,305
  • 21
  • 33
19

Before writing below queries first get 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 one from below options :-

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

To show all collections content or data use below listed code which had been posted 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
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Amit Kumar
  • 797
  • 7
  • 10
14

I prefer another approach if you are using mongo shell:

First as the another answers: 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" : 19
        }
]

You can use similar approach with db.getCollectionInfos() it is pretty useful if you have so much data & helpful as well.

Community
  • 1
  • 1
Juan de Dios
  • 2,683
  • 2
  • 20
  • 24
  • 2
    Use `count()` instead of `find()`: `db.getCollectionNames().map( (name) => ({[name]: db[name].count()}) )` – hallman76 Jan 30 '20 at 19:47
13

This will do:

db.getCollectionNames().forEach(c => {
    db[c].find().forEach(d => {
        print(c); 
        printjson(d)
    })
})
yunzen
  • 32,854
  • 11
  • 73
  • 106
10

This way:

db.collection_name.find().toArray().then(...function...)
0

Thumbs up to Juan de Dios and yunzen, for actually answering the original question of getting ALL collections and, for each, ALL documents.

I'm going to almost clone Juan de Dios's answer, because imho he almost did it:

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

Obviously, this will pour thousands of lines on your tty (depending on the length of your collections) and I believe Juan's option to print the length relates to this.

Also, Juan's solution is functional, so I prefer it (and that's why I'm cloning it here) to yonzen's procedural solution, but they both do it.

As for imbatman's remark (on this answer) that you cannot do this via CLI, that's yet another iteration of the ongoing SO trend to answer without knowing what you're talking about, without trying, without reading documentation, only to show activity and confuse newbies. All you see here is CLI.

Ricardo
  • 576
  • 8
  • 10