programing

mongo 쉘에 모든 데이터베이스를 나열하려면 어떻게 해야 합니까?

jooyons 2023. 4. 5. 21:33
반응형

mongo 쉘에 모든 데이터베이스를 나열하려면 어떻게 해야 합니까?

특정 데이터베이스의 모든 컬렉션을 나열하는 방법은 알고 있습니다만, MongoDB 쉘에서 사용 가능한 모든 데이터베이스를 나열하는 방법은 무엇입니까?

mongoDB 콘솔에 모든 데이터베이스를 나열하려면 다음 명령을 사용합니다.show dbs.

mongo shell 명령어에 대한 자세한 내용은 "Mongo shell Quick Reference"를 참조하십시오.

데이터베이스 목록의 경우:

show databases
show dbs

테이블/수집 목록의 경우:

show collections
show tables
db.getCollectionNames()

MongoDB 쉘 버전 3.0.5의 경우 셸에 다음 명령을 삽입합니다.

db.adminCommand('listDatabases')

또는 다음 중 하나를 선택합니다.

db.getMongo().getDBNames()

명령줄 문제에서

mongo --quiet --eval  "printjson(db.adminCommand('listDatabases'))"

그 결과,

{
    "databases" : [
        {
            "name" : "admin",
            "sizeOnDisk" : 978944,
            "empty" : false
        },
        {
            "name" : "local",
            "sizeOnDisk" : 77824,
            "empty" : false
        },
        {
            "name" : "meteor",
            "sizeOnDisk" : 778240,
            "empty" : false
        }
    ],
    "totalSize" : 1835008,
    "ok" : 1
}

다운스트림 처리를 위해 모든 데이터베이스의 수직 목록을 가져오려면 다음과 같이 하십시오.

mongo --quiet --eval  "printjson(db.adminCommand('listDatabases'))" | jq  '.databases[].name' | tr -d '"' 

아래 출력은 모든 데이터베이스 목록입니다.

admin
local
meteor

셸에 mongodb 데이터베이스를 나열하려면

 show databases     //Print a list of all available databases.
 show dbs   // Print a list of all databases on the server.

몇 가지 기본 명령어 추가

use <db>    // Switch current database to <db>. The mongo shell variable db is set to the current database.
show collections    //Print a list of all collections for current database.
show users  //Print a list of users for current database.
show roles  //Print a list of all roles, both user-defined and built-in, for the current database.

MongoDB 쉘의 모든 dbs를 나열하기 위한 명령어가 몇 가지 있습니다.

먼저 'mongo' 명령을 사용하여 Mongodb 쉘을 실행합니다.

몽고

그런 다음 다음 다음 명령 중 하나를 사용하여 모든 DB를 나열합니다.

  • show dbs
  • show databases의
  • db.admin Command ( { listDatabases : 1, nameOnly : true} )

스냅샷

자세한 것은 이쪽을 봐 주세요.

감사해요.

admin()/others가 작동하지 않는 솔루션을 하나 찾았습니다.

const { promisify } = require('util');
const exec = promisify(require('child_process').exec)
async function test() {
  var res = await exec('mongo  --eval "db.adminCommand( { listDatabases: 1 }         
)" --quiet')
  return { res }
}

test()
  .then(resp => {
    console.log('All dbs', JSON.parse(resp.res.stdout).databases)
  })
test()

MongoDB 공식 문서에 따르면 MongoDB 4+의 경우 데이터베이스 이름을 나열할 수 있습니다.db.adminCommand( { listDatabases: 1, , nameOnly: true } )admin 데이터베이스와 대조합니다.

MongoDB Cloud를 사용하는 경우 먼저 MongoDB 배치에 연결해야 합니다.이 경우 이 명령을 실행할 수 있습니다.mongosh "mongodb+srv://cluster0.<your-connection-string>.mongodb.net" --apiVersion 1 --username <your-user-name>를 참조해 주세요.

Atlas atlas-xxxxxx-shard-0 [primary] test> db.adminCommand({listDatabases:1 , nameOnly: true})
{
  databases: [
    { name: 'sample_airbnb' },
    { name: 'sample_analytics' },
    { name: 'sample_geospatial' },
    { name: 'sample_guides' },
    { name: 'sample_mflix' },
    { name: 'sample_restaurants' },
    { name: 'sample_supplies' },
    { name: 'sample_training' },
    { name: 'sample_weatherdata' },
    { name: 'admin' },
    { name: 'local' }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: xxxxxxxxxx, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "hex"), 0),
      keyId: Long("xxxxxxxxxxxxx")
    }
  },
  operationTime: Timestamp({ t: xxxxxxxxxx, i: 1 })
}

언급URL : https://stackoverflow.com/questions/25947929/how-to-list-all-databases-in-the-mongo-shell

반응형