Mongodb create new database

To create a new database in MongoDB, you can use the following methods:

Method 1: Using the MongoDB Shell

  1. Open the MongoDB shell by running the command mongo in your terminal or command prompt.
  2. 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

  1. Open MongoDB Compass, a GUI tool for interacting with MongoDB.
  2. Click on the "Databases" tab in the left-hand menu.
  3. Click on the "Create Database" button.
  4. Enter the name of the database you want to create in the "Database name" field.
  5. Click on the "Create" button to create the database.

Method 3: Using the MongoDB Command-Line Tool

  1. Open a terminal or command prompt and navigate to the directory where your MongoDB installation is located.
  2. 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.