Open In App

MongoDB insertOne() Method - db.Collection.insertOne()

Last Updated : 24 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The insertOne() method in MongoDB is used to add a single document to a collection. It is a simple and fast way to add data, such as text, numbers, or arrays, into a collection. The method returns details about the inserted document, including its unique _id.

Syntax: 

db.Collection_name.insertOne(

<document>,

{

writeConcern: <document>

}

)

In the above syntax:

  • <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()

Let’s go over a 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

 Here, 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.

Query:

db.student.insertOne({Name: "Akshay", Marks: 500})

Output:

Explanation: As shown above, MongoDB has inserted the document with a new ObjectId automatically generated for the _id field.

Example 2: Insert a Document Specifying an _id Field

Here, we are inserting a document whose unique id is Stu102, name is Vishal, and marks is 230 in the student collection

Query:

db.student.insertOne({_id: "Stu102", Name: "Vishal", Marks: 230})

Output:

file

Explanation: Here, we specified the _id as "Stu102", and MongoDB inserts the document successfully.

Example 3: Handling Write Concern with insertOne()

If we want to specify a custom write concern, we can add an optional writeConcern parameter. For instance, this can be useful when we want to ensure data is written to multiple nodes before considering it committed.

Query:

db.student.insertOne(
  { Name: "John", Marks: 420 },
  { writeConcern: { w: 1, j: true, wtimeout: 5000 } }
)

Common Errors with insertOne()

While the insertOne() method is quite efficient, you might encounter some errors when:

  • Duplicate _id: If the _id field is already present in the collection, MongoDB will throw a DuplicateKeyError. Always ensure the _id field is unique unless you’re ok with the default behavior of automatic _id generation.
  • Write Concern Errors: If you’ve set a custom write concern and it cannot be satisfied (e.g., not enough replicas), you might get a WriteConcernError.

Explore