To create a database in MongoDB, follow the steps below:
1. Start by opening a new Command prompt.
2. Find your MongoDB bin directory and change the command prompt’s current directory to MongoDB’s bin directory.
3. Start the MongoDB server by typing the command `mongod` in the command prompt and hit enter. If the MongoDB server starts successfully, the shell displays waiting for connections message.
4. Now, open a new command prompt and change the current directory to MongoDB’s bin directory.
5. Type `mongo` in the second command prompt and hit enter. This will start the MongoDB shell.
6. Now you can create a new database. The command to create a new database is `use DATABASE_NAME`. Replace DATABASE\_NAME with the name you want to use for your database. If the database doesn’t already exist, a new one will be created. For example, `use myNewDB`
Note: In MongoDB, a database is not created until it gets content, so if this is your first time creating a database, you should also create a collection and insert a document into it. Here’s how to do it:
1. After creating a database, you need to create a collection inside it using the command: `db.createCollection(‘COLLECTION_NAME’)` Replace COLLECTION\_NAME with the name you want to use for your collection. For example, `db.createCollection(‘myFirstCollection’)`
2. After creating a collection, insert a document into it using the command: `db.COLLECTION_NAME.insert({name: “First Document”})` Replace COLLECTION\_NAME with the name of your collection. In this case, `db.myFirstCollection.insert({name: “First Document”})`
3. You can view your database by using the command `show dbs`.
4. You can also view the documents in your collection by using the command `db.COLLECTION_NAME.find()`. Replace COLLECTION\_NAME with the name of your collection, so the command becomes `db.myFirstCollection.find()`.