MongoDB Show all contents from all collections

前端 未结 8 1767
刺人心
刺人心 2020-12-07 07:45

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

Is the only way to show one by one?

8条回答
  •  离开以前
    2020-12-07 08:25

    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.

提交回复
热议问题