Using the Mongo Shell: Using Databases

In the following subsections we shall discuss getting information about a database, creating a database, and dropping a database.

1. Getting Databases Information

The Mongo shell has a variable called db, which references the current database. By default when the Mongo shell is started using the mongo command the test database becomes the current database as discussed in an earlier section. The show dbs command lists all the databases on the server. The db.stats() Mongo shell helper method lists the stats on the current database. The listDatabases command lists all the databases including the total size and size for each database and if the database is empty. Some commands – the administrative commands – such as the listDatabases command must be run on the admin database.

>show dbs

>db.stats()

>use admin

>db.runCommand({listDatabases : 1})

The output from the preceding commands gets displayed in the Mongo shell as shown in Figure 2-9.

2. Creating a Database Instance

A MongoDB database instance is created implicitly when a command is sent to the database instance and an operation is performed such as creating a collection. The use <db> command may be used to select the current database even if the database does not already exist, but the use <db> command does not create a nonexistent database. To create the database an operation must be run on the database. For example, list all databases with the show dbs command. Subsequently select the current database as a nonexistent database mongodb using the use mongodb command. And subsequently create a collection called catalog using the db.createCollection (name,options) helper method.

>show dbs

>use mongodb

>show dbs

>db.createCollection(“catalog”)

>show dbs

If the show dbs command is run after the use mongodb command the mongodb database does not get listed, but if the show dbs command is run after the db.createCollection() command the mongodb database gets listed as shown in Figure 2-10.

The db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism) command may be used to copy a database to another database. The target database gets created. For example, copy the database local to a new database called catalog.

db.copyDatabase(‘local’, ‘catalog’)

If the show dbs command is run before and after the db.copyDatabase() command, the command run after it lists the catalog database as shown in Figure 2-11.

3. Dropping a Database

To remove the current database use the db.dropDatabase() method. The current database is the database set with the use <db> command. Even after dropping a database the current database name is not changed, and if a new collection is created, it is created in a database of the same name as the dropped database using new data files. For example list all databases using the show dbs command. Subsequently set the current database as one of the databases listed, for example, database catalog. The database chosen for deletion could be different and does not have to be called catalog. Subsequently invoke the db.dropDatabase() method to drop the current database, which is the database mongo. If subsequently the show dbs command is invoked the catalog database does not get listed.

>show dbs

>use catalog

>db.dropDatabase()

>show dbs

The db.dropDatabase() method returns a document with fields “dropped” and “ok.” The “dropped” field value is the database dropped, and the “ok” field value of 1 indicates the method returned without error as shown in Figure 2-12.

To quit the Mongo shell run the quit() command or the exit command.

>quit()

The Mongo shell session gets ended as shown in Figure 2-13.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *