Mongo/Databases
From charlesreid1
Contents
Getting Info About Databases
Getting Database Info with Mongo Shell
Start the mongo shell:
$ mongo >
To list all available databases, use any of these commands:
> db.adminCommand('listDatabases') > db.getMongo().getDBNames() > show databases > show dbs
Show all tables and collections in a database:
> use test_db > show tables > show collections > db.getCollectionNames()
Command line
To bypass the javascript shell and run commands directly from the command line, run the commands using --eval:
mongo <dbname> --eval "db.getMongo().getDBNames()" mongo <dbname> --eval "db.getCollectionNames()" mongo <dbname> --eval "db.dropDatabase()"
Removing (Dropping) Databases
To remove (drop) a database, use the db.dropDatabase()
function.
Dropping Databases with Mongo Shell
From the mongo shell:
$ mongo > use test_db > db.dropDatabase()
Adding Databases
MongoDB has a simple approach to adding databases: just act like the database exists already, and if it does not, MongoDB will initialize it for you.
Adding Databases with Mongo Shell
After starting the mongo shell, I can create a test_db database by saying "use test_db":
$ mongo > use test_db
You can always see the available databases using the show dbs
command:
> show dbs local 0.78125GB test_db 0.23012GB
Note: a database with zero records in it will not show up when you run the show dbs
command. Add records to see the database.
Resources
Administration commands: https://docs.mongodb.com/manual/reference/command/nav-administration/