📜  mongo shell change db - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:32:56.197000             🧑  作者: Mango

Mongo Shell Change DB - Shell Bash

MongoDB is a popular NoSQL database that is widely used by many applications. One of the key features of MongoDB is its ability to store and manage large amounts of unstructured data in a flexible and scalable manner.

When working with MongoDB, the mongo shell is often the tool of choice for developers and administrators, as it provides a powerful command-line interface for interacting with MongoDB.

In this article, we will explore how to change the current database in the mongo shell using shell bash commands.

Connect to MongoDB

First, we need to connect to our MongoDB instance before we can change the current database. To do this, we can use the following command in the shell:

$ mongo

This will start the mongo shell and connect to the default MongoDB instance running on localhost.

List Databases

To see the list of available databases, we can use the show dbs command. This will display a list of all the databases that are available on the MongoDB instance that we are connected to.

> show dbs
admin   0.000GB
local   0.000GB

As you can see, this command will list all the currently available databases, along with their sizes.

Change the Current Database

To change the current database in the mongo shell, we can use the use command. This command takes the name of the database that we want to switch to as an argument.

For example, if we want to change to the mydb database, we can use the following command:

> use mydb

After running this command, the prompt will update to reflect the new database we are currently using.

> 

Now, any commands that we run will be executed in the mydb database.

Summary

In this article, we have covered how to change the current database in the mongo shell using shell bash commands. By using the use command, we can easily switch between databases and execute commands in the correct context.

Remember that when you change the current database, any existing variables that you had defined will still be available, but they will operate in the context of the new database.

We hope that you found this article useful, and that it helps you to become more proficient when working with MongoDB and the mongo shell. Happy coding!