MongoDB Shell: How to select a database to use

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Welcome to your guide on how to select a database in the MongoDB Shell. In this tutorial, we will explore the use of the MongoDB Shell to interact with your MongoDB instance effectively. If you are new to MongoDB, it is a document-based NoSQL database that provides high performance and easy scalability. As database selection is crucial for performing any operation within the database, mastering this step is foundational for anyone who works with MongoDB.

Prerequisites

  • MongoDB server installed and running
  • Access to the MongoDB Shell (use command mongo)

Basic Database Selection

To begin, let’s talk about how to select a database for use in MongoDB Shell. The command to switch to a specific database is simple:

use <database_name>

This command will switch the context of the shell to the specified database. If the database does not exist, MongoDB will not create a new database at this time. A new database is created once you insert data into it.

For example, to switch to a database named ‘testdb’, you would enter:

use testdb

This will output:

switched to db testdb

If ‘testdb’ does not exist, it will still say “switched to db testdb” but the database is not actually created until data is inserted into a collection.

Listing Available Databases

Before selecting a database, you might want to know which databases are available in your MongoDB instance. To list all databases, use the show dbs command:

show dbs

This will display all the existing databases along with their sizes on disk.

Creating a New Database

If you want to create a new database and switch to it immediately, you follow the same basic command use <database_name>. To actually create the database, besides switching to it, you’ll need to insert some data:

use newdb
db.newcollection.insert({"key": "value"})

Where ‘newdb’ is the name of your new database and ‘newcollection’ is a collection that you want to create within ‘newdb’. This will both switch to ‘newdb’ and create it by adding a new document to ‘newcollection’.

Checking the Current Database

If you’ve lost track of which database you’re currently using in the MongoDB Shell, you can easily check by using the db command:

db

This will return the name of the database you are currently connected to.

Advanced Database Operations

In addition to basic database selection, MongoDB Shell offers a rich set of commands for more advanced operations. Let’s explore some of those commands.

Using Javascript to Interact with Databases

Since the MongoDB Shell is a JavaScript interpreter, you can use JavaScript functions to interact with the database:

use testdb
var collections = db.getCollectionNames();
for(var i = 0; i < collections.length; i++) {
    print(collections[i]);
}

This snippet first selects ‘testdb’, retrieves the names of all collections in it and prints each collection name iteratively.

Database Drop

To remove a database you are currently using, you would execute the db.dropDatabase() command:

use obsoleteDB
db.dropDatabase()

This command will completely remove the ‘obsoleteDB’ and all of its collections and documents.

Note: This operation cannot be undone and should be done with extreme caution.

Conclusion

In this tutorial, you’ve learned how to select and switch between databases in the MongoDB Shell. Through clear examples and explanations, we’ve covered several tasks, from listing databases to dropping them. Remember, selecting the right database is crucial for any data operation you are planning to perform in MongoDB. Hence, understanding these commands is fundamental to effective database management and manipulation. Keep practicing, and soon these commands will become second nature to you!