Mongodb create new database
To create a new database in MongoDB, you can use the following methods:
Method 1: Using the MongoDB Shell
- Open the MongoDB shell by running the command
mongo
in your terminal or command prompt. - Once you are in the shell, type the following command to create a new database:
use mydatabase
Replace
mydatabase
with the name of the database you want to create.
Method 2: Using the MongoDB Compass GUI
- Open MongoDB Compass, a GUI tool for interacting with MongoDB.
- Click on the "Databases" tab in the left-hand menu.
- Click on the "Create Database" button.
- Enter the name of the database you want to create in the "Database name" field.
- Click on the "Create" button to create the database.
Method 3: Using the MongoDB Command-Line Tool
- Open a terminal or command prompt and navigate to the directory where your MongoDB installation is located.
- Run the following command to create a new database:
mongo --eval "use mydatabase"
Replace
mydatabase
with the name of the database you want to create.
Method 4: Using a programming language (e.g. Python, Java, Node.js)
You can also create a new database using a programming language. For example, in Python:
from pymongo import MongoClient
client = MongoClient()
db = client["mydatabase"]
Replace mydatabase
with the name of the database you want to create.
Note: If the database does not exist, MongoDB will create it automatically when you try to use it.