Before you can insert data into MongoDB, you need to create both a MongoDB database and collection.
1. Create a MongoDB database: The `use DATABASE_NAME` command is written to create a database in MongoDB. If the database does not exist, MongoDB will create it.
```
use myDb
```
1. Create a MongoDB Collection: The `db.createCollection(name, options)` is used to create collection.
```
db.createCollection(‘myCollection’)
```
1. Insert Data into MongoDB
After creating both a MongoDB database and collection, you can insert data into MongoDB.
- Insert One Document: To insert a single document into a collection, you can use the `insertOne()` method.
```
db.myCollection.insertOne({name: ‘John’, age: 30, profession: ‘Developer’})
```
- Insert Multiple Documents: To insert multiple documents into a collection you can use the `insertMany()` method.
```
db.myCollection.insertMany([
{name: ‘John’, age: 30, profession: ‘Developer’},
{name: ‘Jane’, age: 28, profession: ‘Designer’},
{name: ‘Doe’, age: 26, profession: ‘Tester’}
])
```
These commands will insert one or many documents to the `myCollection` collection. If the collection doesn’t exist in the database, then MongoDB will create this collection and then insert a document into it.
In MongoDB, you don’t need to create a collection. MongoDB creates collections automatically, when you insert some documents.