I will consider that you have?learned about MongoDB Data Modeling if not then kindly check my blog on MongoDB Data Modeling. Following that, we must begin using MongoDB. To do so, we must first understand how MongoDB creates databases. In this tutorial, we will look at how to construct a database in MongoDB.
So, let's get started with the MongoDB database creation lesson.
Create a Database in MongoDB
If you're looking for a database creation command, you should stop right now. This is due to the fact that MongoDB does not include a command for creating a database. We don't even build a database in MongoDB.
Unlike SQL, which requires us to establish a database, tables, and then manually insert the values, MongoDB creates the database for us. You only need to save a value with the desired name in the defined collection.
You don't even have to say anything about creating a database. You can, however, manually build collections. We'll talk about it later.
The ?use? Command
If there is no database, use the following command to define its name, and the database is created if it does not already exist.
use database_name
Let's imagine the database's name is "codesolutionstuff."
>use codesolutionstuff
Switched to db codesolutionstuff
Check Selected Database
You can already check the database you choose. To do so, execute this command.
>db codesolutionstuff
List Database
If you don't know which databases are already in the system, you can list them and see if they exist. Simply use the following command to accomplish this.
>show dbs
local 0.52938GB
test 0.49231GB
If your database does not appear in the list, it has not yet been created. You must save at least one document in order to build a database.
Save a Document
To put a document into the database, use the following command.
> db.user.insert({name: "Shailesh", age: 23})
WriteResult({ "nInserted" : 1 })
> show dbs
local 0.52938GB
codesolutionstuff 0.49231GB
test 0.49231GB
Your database, named "codesolutionstuff," has now been created.
This concludes the MongoDB Create Database Tutorial. We hope you found our explanation helpful.
Conclusion
As a result, we saw in this MongoDB Tutorial that MongoDB has its own capability for creating databases. We don't need to put in as much work to construct a database.
We now understand how MongoDB creates database.