MongoDBを使用する場合、データベース内のコレクションを一覧表示する方法は複数あります。
MongoDBデータベースのコレクションのリストを取得する4つの方法は次のとおりです。
show collectionsコマンド-
listCollectionsコマンド -
db.getCollectionNames()方法 -
db.getCollectionInfos()方法
show collections コマンド
mongoシェルを使用している場合、コレクションのリストを取得する最も簡単な方法は、show collectionsを使用することです。 指図。このコマンドは、現在のデータベース内のコレクションとビューのリストを取得します。
例:
show collections 結果:
employees pets pettypes products system.views
この場合、5つの結果があります。見ただけではわかりませんが、pettypes 実際にはビューです。その他はコレクションです。
system.views コレクションは、データベース内の各ビューに関する情報を含むシステムコレクションです。
返される実際のコレクションは、アクセスレベルによって異なります:
- 必要なアクセス権を持つユーザーの場合、
show collectionsデータベースの非システムコレクションを一覧表示します。 - 必要なアクセス権がないユーザーの場合、
show collectionsユーザーが特権を持っているコレクションのみを一覧表示します。
listCollections コマンド
listCollections 管理コマンドは、データベース内のコレクションとビューの名前とオプションを返します。情報をドキュメントの形式で返します。
例:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } ) 結果:
{
"cursor" : {
"id" : NumberLong(0),
"ns" : "PetHotel.$cmd.listCollections",
"firstBatch" : [
{
"name" : "employees",
"type" : "collection"
},
{
"name" : "system.views",
"type" : "collection"
},
{
"name" : "pets",
"type" : "collection"
},
{
"name" : "products",
"type" : "collection"
},
{
"name" : "pettypes",
"type" : "view"
}
]
},
"ok" : 1
} ドキュメントには、コレクション情報へのカーソルを作成するための情報が含まれています。
今回は、コレクションとビューを確認できます。
次のようなコマンドを実行することもできます:
db.runCommand( { listCollections: 1.0 } )
そうすることで、コレクションに関するより多くの情報が得られます。 db.getCollectionInfos()を参照してください 以下の例で、そのように実行したときに返されるデータを確認します(db.getCollectionInfos() メソッドはlistCollectionsのラッパーです 。
db.getCollectionNames() 方法
db.getCollectionNames() メソッドは、現在のデータベース内のすべてのコレクションとビューの名前を含む配列を返します。アクセス制御を使用して実行している場合は、ユーザーの権限に応じたコレクションの名前を返します。
例:
db.getCollectionNames() 結果:
[ "employees", "pets", "pettypes", "products", "system.views" ]
db.getCollectionInfos() 方法
db.getCollectionInfos() メソッドは、現在のデータベースの名前やオプションなどのコレクションまたはビュー情報を含むドキュメントの配列を返します。結果はユーザーの権限によって異なります。
引数なしで呼び出す例は次のとおりです。
db.getCollectionInfos() 結果:
[
{
"name" : "employees",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("07e89c25-8842-4331-a1a9-96fe0b4745dc")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
},
{
"name" : "pets",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
},
{
"name" : "pettypes",
"type" : "view",
"options" : {
"viewOn" : "pets",
"pipeline" : [
{
"$project" : {
"type" : 1
}
}
]
},
"info" : {
"readOnly" : true
}
},
{
"name" : "products",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 7500544,
"max" : 7000
},
"info" : {
"readOnly" : false,
"uuid" : UUID("cb084959-f374-4f51-bbed-8998c13dcbe2")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
},
{
"name" : "system.views",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("3f458338-0678-4d0f-a0cf-eacbd43c8cad")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
}
]
db.getCollectionInfos()の定義 実際には次のようになります:
db.getCollectionInfos(filter, nameOnly, authorizedCollections)
したがって、filterを使用できます クエリ式に基づいてコレクションのリストをフィルタリングするパラメータ。これは、メソッドによって返される任意のフィールドに適用できます。
nameOnlyを使用することもできます メソッドがコレクションとビューの名前のみを返すように指定するパラメーター。
authorizedCollections trueに設定されている場合のパラメータ nameOnly: trueで使用されます 、アクセス制御が適用されている場合、必要な権限(つまり、データベースに対するlistCollectionsアクション)を持たないユーザーがコマンドを実行できるようにします。この場合、コマンドは、ユーザーが特権を持っているコレクションのみを返します。
db.getCollectionInfos()の使用例 これらのパラメータを使用して:
db.getCollectionInfos( {}, true, true ) 結果:
[
{
"name" : "employees",
"type" : "collection"
},
{
"name" : "pets",
"type" : "collection"
},
{
"name" : "pettypes",
"type" : "view"
},
{
"name" : "products",
"type" : "collection"
},
{
"name" : "system.views",
"type" : "collection"
}
]
これが私がそれを特定の名前だけにフィルターするものです:
db.getCollectionInfos( { name: "pets" }, true, true ) 結果:
[ { "name" : "pets", "type" : "collection" } ] 最後の2つの引数を削除すると、次のようになります。
db.getCollectionInfos( { name: "pets" } ) 結果:
[
{
"name" : "pets",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
}
]