Mongo/Databases: Difference between revisions
From charlesreid1
| Line 40: | Line 40: | ||
=Removing (Dropping) Databases= | =Removing (Dropping) Databases= | ||
To remove (drop) a database, use the <code>db.dropDatabase()</code> function. From the mongo shell: | To remove (drop) a database, use the <code>db.dropDatabase()</code> function. | ||
==Dropping Databases from the Mongo Shell== | |||
From the mongo shell: | |||
<pre> | <pre> | ||
| Line 47: | Line 51: | ||
> db.dropDatabase() | > db.dropDatabase() | ||
</pre> | </pre> | ||
=Adding Databases= | =Adding Databases= | ||
Revision as of 08:16, 31 January 2018
Getting Info About Databases
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 from the 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/