Mongo/Databases: Difference between revisions
From charlesreid1
(Created page with "=Getting Info About Databases= ==Mongo shell== Start the mongo shell: <pre> $ mongo > </pre> To list all available databases, use any of these commands: <pre> > db.adminC...") |
|||
| Line 27: | Line 27: | ||
> db.getCollectionNames() | > db.getCollectionNames() | ||
</pre> | </pre> | ||
==Command line== | |||
To bypass the javascript shell and run commands directly from the command line, run the commands using --eval: | |||
<pre> | |||
mongo <dbname> --eval "db.getMongo().getDBNames()" | |||
mongo <dbname> --eval "db.getCollectionNames()" | |||
mongo <dbname> --eval "db.dropDatabase()" | |||
</pre> | |||
=Removing (Dropping) Databases= | |||
To remove (drop) a database, use the <code>db.dropDatabase()</code> function. From the mongo shell: | |||
<pre> | |||
$ mongo | |||
> use test_db | |||
> db.dropDatabase() | |||
</pre> | |||
[[Category:MongoDB]] | |||
[[Category:Databases]] | |||
Revision as of 08:12, 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. From the mongo shell:
$ mongo > use test_db > db.dropDatabase()