Programing

mongo 셸의 모든 컬렉션을 나열하는 방법은 무엇입니까?

lottogame 2020. 9. 30. 08:38
반응형

mongo 셸의 모든 컬렉션을 나열하는 방법은 무엇입니까?


MongoDB 셸에서 사용중인 현재 데이터베이스에 대한 모든 컬렉션을 어떻게 나열합니까?


넌 할 수있어...

JS (쉘) :

db.getCollectionNames()

node.js :

db.listCollections()

비 JS (쉘 전용) :

show collections

비 JS라고 부르는 이유는 다음과 같습니다.

$ mongo prodmongo/app --eval "show collections"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
2016-10-26T19:34:34.886-0400 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell eval):1:5

$ mongo prodmongo/app --eval "db.getCollectionNames()"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
[
    "Profiles",
    "Unit_Info"
]

달콤하고 달콤한 show collections결과물을 원한다면 다음을 수행 할 수 있습니다.

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info

> show collections

명령 줄 도움말 ( help)에 명시된대로 현재 선택된 DB의 모든 컬렉션을 나열합니다 .


사용중인 현재 데이터베이스에 대한 모든 컬렉션을 어떻게 나열합니까?

3 가지 방법

  • show collections
  • show tables
  • db.getCollectionNames()

모든 데이터베이스 를 나열하려면 다음을 수행하십시오 .

show dbs

주어진 데이터베이스를 입력하거나 사용하려면 :

use databasename

모든 컬렉션 을 나열하려면 :

show collections

산출:

collection1  
collection2  
system.indexes

(또는)

show tables

산출:

collection1  
collection2  
system.indexes

(또는)

db.getCollectionNames()

산출:

[ "collection1", "collection2", "system.indexes" ]

주어진 컬렉션을 입력하거나 사용하려면

use collectionname

> show tables

Cameron의 대답과 동일한 결과를 제공합니다.


다른 사람들이 제안한 옵션 외에 :

show collections  //output every collection
show tables
db.getCollectionNames() //shows all collections as a list

There is also another way which can be really handy if you want to know how each of the collections was created (for example it is a capped collection with a particular size)

db.system.namespaces.find()

First you need to use a database to show all collection/tables inside it.

>show dbs
users 0.56787GB
test (empty)
>db.test.help() // this will give you all the function which can be used with this db
>use users
>show tables //will show all the collection in the db

you can use show tables or show collections


Try:

help // To show all help methods
show dbs  // To show all dbs
use dbname  // To select your db
show collections // To show all collections in selected db

The command used for displaying all the collection in the mongoDb database is

show collections 

Before running the show collections command you have to select the database

use mydb //mydb is the name of the database being selected

To see all the databases you can use the command

show dbs // shows all the database names present 

For more info visit this link : http://docs.mongodb.org/manual/tutorial/getting-started/


If you want to show all collections from mongodb shell (command line), use shell helper

show collections

that show all collections for current database. If you want to get all collection list from your application then you can use mongodb database method

db.getCollectionNames()

For more info mongodb shell helper you can seee http://docs.mongodb.org/manual/reference/mongo-shell/


The following commands on mongoshell are common

show databases
show collections

Also,

show dbs
use mydb
db.getCollectionNames()

Sometimes it's useful to see all collections as well as the indexes on the collections which are part of the overall namespace:

Here's how you would do that:

 db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
print("Indexes for " + collection + ":");
printjson(indexes);
});

Between the 3 commands and this snippet you should be well covered!


I think one of the biggest confusions is the difference between what you can do with mongo (or an interactive/hybrid shell) vs. mongo --eval (or a pure javascript shell). I keep these helpful documents handy:

Here is an example of scripting what you might otherwise do with show commands:

# List all databases and the collections in them

mongo --eval "
    db.getMongo().getDBNames().forEach(
        function(v, i){
            print(
                v + '\n\t' +
                db.getSiblingDB(v).getCollectionNames().join('\n\t')
            )
        }
    )
"

Note: That works really well as a oneliner. (But looks terrible on StackOverflow.)

mongo --eval "db.getMongo().getDBNames().forEach(function(v, i){print(v+'\n\t'+db.getSiblingDB(v).getCollectionNames().join('\n\t'))})"

On >=2.x, you can do

db.listCollections()

On 1.x you can do

db.getCollectionNames()

For switch to the database. by:- use {your_database_name} example:

use friends

where friends is the name of your database.

then write:-

db.getCollectionNames()
show collections

this will give you the name of collections.


List all collections from the mongo shell :

  • db.getCollectionNames()
  • show collections
  • show tables

Note : Collections will show from current database where you are in currently


> show dbs        
anuradhfirst  0.000GB
local         0.000GB
> use anuradhfirst
switched to db anuradhfirst
> show collections
record
  • connect with mongo database using mongo, this will start the connection.
  • then run show dbs command, this will show you all exiting/available database.
  • then select the database you want.in above it is anuradhfirst, then run use anuradhfirst.this will switch to the database you want.
  • then run show collections command, this will show all the collections inside your selected database.

show collections

this command usually works on mongo shell once you have switched to the database.


For MongoDB 3.0 deployments using the WiredTiger storage engine, if you run db.getCollectionNames() from a version of the mongo shell before 3.0 or a version of the driver prior to 3.0 compatible version, db.getCollectionNames() will return no data, even if there are existing collections.

For further details, please refer to this


 1. show collections; //Display all collection
 2. show tables     //Display all collection
 3. db.getCollectionNames();   // Retuen array of collection Example :[ "orders", "system.profile" ]

Details information of every collection

db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
  • For users with the required access (privileges that grant listCollections action on the database), the method lists the names of all collections for the database.
  • For users without the required access, the method lists only the collections for which the users has privileges. For example, if a user has find on a specific collection in a database, the method would return just that collection.

I use listCollections (supports mongo 3.0 and up) for this purpose.

example:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true });

To fetch more info like index of the collection:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: false });

To print just the collection names:

db.runCommand({ listCollections: 1, filter: {}, nameOnly: true }).cursor.firstBatch.forEach(v => {print(v.name)})

I feel this provide more flexibility.

read more: https://docs.mongodb.com/manual/reference/command/listCollections/


use following command from mongo shell :- show collections

참고URL : https://stackoverflow.com/questions/8866041/how-to-list-all-collections-in-the-mongo-shell

반응형