There are two ways to create a new database in MongoDB:
Using the MongoDB shell: The MongoDB shell is a powerful tool that can be used to interact with a MongoDB database. To create a new database using the MongoDB shell, use the following command:
Code snippet
use <database_name>
where:
<database_name> is the name of the database to create.
For example, the following command creates a database named my_database:
Code snippet
use my_database
Using the MongoDB driver: The MongoDB driver is a library that can be used to interact with a MongoDB database from a variety of programming languages. To create a new database using the MongoDB driver, use the following method:
Code snippet
db.createDatabase( <database_name> )
where:
<database_name> is the name of the database to create.
For example, the following code creates a database named my_database using the Python MongoDB driver:
Code snippet
from pymongo import MongoClient
client = MongoClient()
db = client.my_database
Once a database is created, you can create collections in it, insert documents into the collections, and query the documents.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are two ways to create a new database in MongoDB:
Code snippet
where:
For example, the following command creates a database named my_database:
Code snippet
Code snippet
where:
For example, the following code creates a database named my_database using the Python MongoDB driver:
Code snippet
Once a database is created, you can create collections in it, insert documents into the collections, and query the documents.