Problem Statement
How do you create a new database in MongoDB?
Explanation
You create a database in MongoDB by using the use command followed by the database name. MongoDB creates the database implicitly when you first store data in it.
If you run use mydb, MongoDB switches to that database, and when you insert your first document into a collection, the database is actually created on disk. This lazy creation approach means you do not need explicit create database commands. The database only takes up space once it contains data.
Code Solution
SolutionRead Only
// Switch to database (creates if doesn't exist)
use mydatabase
// Database is actually created when you insert data
db.users.insertOne({ name: "Alice" })
// Show all databases
show dbs