Last Updated: 30 May, 2023
To create a database in MongoDB, we can use the use command followed by the name of the database we want to create.
The use command is also used to switch to a specific database, and if the database does not exist, MongoDB will create it when we start adding data.
Here's an syntax and example of using the use command to create a new database or switch to an existing database:
Syntax
use database_name
Example
use schoolDB
In this example, "schoolDB" is the name of the database we want to create or switch to. If the database already exists, MongoDB will switch to it. If it doesn't exist, MongoDB will create it when we start adding data.
A newly created database will not be visible until it has at least one document in it.
Remember, MongoDB is a schema-less database, so we don't need to define a schema beforehand. The structure of the documents can vary within a collection, allowing flexibility in data modeling.
In MongoDB, test is the default database.
That's all, guys. I hope this MongoDB article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com