mongodb create database
In MongoDB, a new database is created automatically when data is first inserted into it. However, you can also create a new database explicitly using the use
command.
Here are the steps to create a new database in MongoDB:
Start the MongoDB server: Open a terminal or command prompt and start the MongoDB server by running the
mongod
command.Open the MongoDB shell: Open another terminal or command prompt and run the
mongo
command to open the MongoDB shell.Switch to the desired database: To switch to a database, use the
use
command followed by the name of the database. For example, to switch to a database named "mydb", run the following command:
use mydb
If the database does not exist, MongoDB will create it when data is first inserted.
- Insert data: Insert data into the database using the
insert
command. For example, to insert a document into a collection named "mycollection", run the following command:
db.mycollection.insert({name: "John", age: 30})
This will create a new document in the "mycollection" collection with the fields "name" and "age".