MongoDB Show all contents from all collections

前端 未结 8 1775
刺人心
刺人心 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:15

    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.

提交回复
热议问题