Mongo/Databases: Difference between revisions
From charlesreid1
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
=Getting Info About Databases= | =Getting Info About Databases= | ||
==Mongo | ==Getting Database Info with Mongo Shell== | ||
Start the mongo shell: | Start the mongo shell: | ||
| 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 with Mongo Shell== | |||
From the mongo shell: | |||
<pre> | <pre> | ||
| Line 48: | Line 52: | ||
</pre> | </pre> | ||
=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": | |||
<pre> | |||
$ mongo | |||
> use test_db | |||
</pre> | |||
You can always see the available databases using the <code>show dbs</code> command: | |||
<pre> | |||
> show dbs | |||
local 0.78125GB | |||
test_db 0.23012GB | |||
</pre> | |||
Note: a database with zero records in it will not show up when you run the <code>show dbs</code> command. Add records to see the database. | |||
=Resources= | |||
Administration commands: https://docs.mongodb.com/manual/reference/command/nav-administration/ | |||
[[Category:MongoDB]] | [[Category:MongoDB]] | ||
[[Category:Databases]] | [[Category:Databases]] | ||
Latest revision as of 08:16, 31 January 2018
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/