collection_name: It refers to the name of the collection where the document will be inserted (the collection is created automatically if it does not exist).
<document>: The document we want to insert. A document is a set of key-value pairs similar to a JSON object.
writeConcern: If we need to specify a custom write concern (e.g., to ensure the data is written to multiple nodes), you can include this option.
Return Value of insertOne()
The insertOne() method returns the following:
Acknowledgement: It returns acknowledged: true if the write concern was enabled.
InsertedId: This field contains the _id value of the inserted document
Examples of MongoDB insertOne()
Here are few examples to understand how insertOne() works in MongoDB. In the following examples, we are working with:
Database: gfg
Collection: student
Document: No document but, we want to insert in the form of the student name and student marks.
Example 1: Insert a Document without Specifying an _id Field
We are inserting the document whose name is Akshay and marks is 500 in the student collection. MongoDB will automatically assign a unique _id field to this document.