Last Updated : 23 Jul, 2025
MongoDB is a powerful NoSQL database known for its flexibility, scalability, and performance. When working with MongoDB, one of the most common tasks is inserting data into collections. The insertOne()
method is an essential tool in this process.
In this article, We will learn about the MongoDB insertOne() Method, explain how to use it effectively, and showcase several examples. Whether you’re using MongoDB in the shell, Node.js, or Python, you’ll find this guide helpful.
What is the MongoDB insertOne() Method?The MongoDB insertOne()
method is used to add a single document to a collection. It adds the document to the specified collection and assigns it a unique _id
if one is not provided. This insertOne() method is part of the MongoDB driver for various programming languages and can be used in MongoDB Shell, Node.js, Python and other environments.
Syntax:
db.Collection_name.insertOne(
<document>,
{
writeConcern: <document>
}
)
Key Terms
<document>
: The document we want to insert. A document is a set of key-value pairs similar to a JSON object.writeConcern
(optional): 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.The insertOne()
method returns the following:
acknowledged: true
if the write concern was enabled._id
value of the inserted documentLet’s go over a few examples to understand how insertOne()
works in MongoDB. In the following examples, we are working with:
_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.
_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:
Explanation: Here, we specified the _id
as "Stu102"
, and MongoDB inserts the document successfully.
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(Common Errors with insertOne()
{ Name: "John", Marks: 420 },
{ writeConcern: { w: 1, j: true, wtimeout: 5000 } }
)
While the insertOne()
method is quite efficient, you might encounter some errors when:
_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.WriteConcernError
._id
, if you provide a custom _id
, make sure it's unique to avoid conflicts.insertMany()
for better performance.The insertOne()
method in MongoDB is essential for adding single documents to a collection, offering features such as automatic _id
generation, write concern options, and support for multi-document transactions. By understanding its syntax and parameters, users can efficiently insert data while ensuring its uniqueness and integrity within the collection.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4