回答:
ターミナル/コマンドラインに入ったら、次のように使用したいデータベース/コレクションにアクセスします。
show dbs
use <db name>
show collections
コレクションを選択し、次のように入力して、そのコレクションのすべてのコンテンツを表示します。
db.collectionName.find()
詳細については、MongoDBクイックリファレンスガイドをご覧ください。
db.collectionName.find().pretty()
db["collection-name"].find()
ステップ1:すべてのデータベースを表示します。
show dbs
ステップ2:データベースを選択する
use your_database_name
ステップ3:コレクションを表示する
show collections
これにより、選択したデータベースのすべてのコレクションが一覧表示されます。
ステップ4:すべてのデータを確認する
db.collection_name.find()
または
db.collection_name.find().pretty()
db.<collection_name>.find();
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
}
このスクリプトはあなたが望むものを得るかもしれないと思います。各コレクションの名前を出力し、その要素をjsonに出力します。
以下のクエリを書く前に、最初にコマンドまたはPowerShellにアクセスします
TYPE:
mongo //To get into MongoDB shell
use <Your_dbName> //For Creating or making use of existing db
すべてのコレクション名をリストするには、以下のオプションのいずれかを使用します:-
show collections //output every collection
OR
show tables
OR
db.getCollectionNames() //shows all collections as a list
すべてのコレクションのコンテンツまたはデータを表示するには、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
}
こちらです:
db.collection_name.find().toArray().then(...function...)
mongo
シェルを使用している場合は、別の方法をお勧めします。
まず別の答えとして:use my_database_name
次に:
db.getCollectionNames().map( (name) => ({[name]: db[name].find().toArray().length}) )
このクエリは次のようなものを表示します:
[
{
"agreements" : 60
},
{
"libraries" : 45
},
{
"templates" : 9
},
{
"users" : 18
}
]
db.getCollectionInfos()
データが非常に多い場合は、同様のアプローチを使用できます。
count()
の代わりに、find()
:db.getCollectionNames().map( (name) => ({[name]: db[name].count()}) )