MongoDB – Insert Document:

In MongoDB, Document is nothing but a key-value pairs.we can you the below commands to insert a document into a collection.

Syntax:

db.collection.insert()

Let us insert a document in collection called “student”.

First check in which intended database you are in and try the below commands

Now Check the inserted document in collection.

If you Observe, we did not insert “_id “, when we insert any values by default mongoDB maintains or creates “_id” field and associated value

db.collection.insertOne() –> New command in version 3.2

This will insert a single document in collection.

Example:

db.collection.insertMany() –> New command in version 3.2

This will insert multiple documents in collection.

Example: