mongoDB コレクションの情報を一覧で取得する

mongoDB コレクションの情報を一覧で取得する

mongoDBで、コレクションの情報を一覧で取得する手順を記述してます。「db.getCollectionInfos()」を実行すれば取得できます。コレクション別に取得することも可能です。

環境

  • OS CentOS Stream release 9
  • MongoDB 6.0.2

手順

コレクションの情報を一覧で取得するには、「db.getCollectionInfos()」を使用します。

db.getCollectionInfos()

実際に、使用してみます。

> use hoge

> db.getCollectionInfos()

[
  {
    name: 'bar',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("15379d09-32d0-4b16-bed0-c62f539fa834")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  },
  {
    name: 'hoge',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("41960d57-9d21-447b-a931-73ec21ceb614")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  },
  {
    name: 'foo',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("517baef1-4633-44df-b8ef-0bb50ad8a748")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  }
]

設定されている情報が一覧でき取得されます。

コレクションを指定

コレクションを指定して取得する場合は、以下のようにコレクションを設定します。

> db.getCollectionInfos({name:"foo"})

[
  {
    name: 'foo',
    type: 'collection',
    options: {},
    info: {
      readOnly: false,
      uuid: new UUID("517baef1-4633-44df-b8ef-0bb50ad8a748")
    },
    idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
  }
]