Last Updated : 23 Jul, 2025
The insert()
method in MongoDB is a fundamental operation used to add new documents to a collection. It allows inserting one or multiple documents in a single execution with MongoDB automatically generating a unique _id
field if not explicitly provided.
In this article, We will learn about the MongoDB Insert() method by understanding its behavior
along with multiple examples to illustrate its usage.
The insert()
method is a fundamental operation in MongoDB that is used to add new documents to a collection. This method is flexible, allowing developers to insert either a single document or multiple documents in a single operation, which can significantly enhance performance and reduce the number of database calls.
When a document is inserted, MongoDB automatically generates a unique _id
field if it is not provided by the user. This automatic generation of the _id
field ensures that each document can be uniquely identified, which is important for maintaining data integrity and facilitating efficient data retrieval.
Key Points
_id
field. If a document is inserted without the _id
field, MongoDB will automatically add it and assign a unique ObjectId
. If you provide an _id
field, its value must be unique to avoid a duplicate key error.insert()
method can also be used within multi-document transactions.insert()
method is deprecated in the MongoDB shell (mongosh). Instead, use insertOne()
and insertMany()
methods to insert new documents into a MongoDB collection.Syntax:
Parametersdb.Collection_name.insert(
<document or [document1, document2,...]>,
{
writeConcern: <document>,
ordered: <boolean>
})
In this section, we will explore practical examples of the db.Collection.insert()
method to demonstrate how to insert new documents into a MongoDB collection. For our examples, we will be working with the following setup:
_id
Field
In this example, we insert a document into the "student" collection with the name "Akshay" and marks "500". By not specifying the _id
field, MongoDB will automatically generate a unique identifier for this document.
Query:
db.student.insert({Name: "Akshay", Marks: 500})
Output:
Explanation:
This operation creates a new document in the student
collection. The document will look like this: { "_id": ObjectId("generated_id"), "Name": "Akshay", "Marks": 500 }
. The _id
field is automatically generated by MongoDB.
In this example, we insert multiple documents into the collection by passing an array of documents to the insert
method. This allows for batch insertion, which is more efficient than inserting documents one at a time.
Query:
db.student.insert([
{Name: "Bablu", Marks: 550},
{Name: "Chintu", Marks: 430},
{Name: "Devanshu", Marks: 499}
])
Output:
Explanation:
This operation inserts three new documents into the student
collection. Each document will have its own automatically generated _id
.
_id
Field
In this example, we insert a document into the student
collection with a specified _id
field. This demonstrates how to manually set the identifier for a document.
Query:
db.student.insert({_id: 102, Name: "Anup", Marks: 400})
Output:
Explanation:
This operation creates a new document with a custom _id
of 102
. The document will look like this: { "_id": 102, "Name": "Anup", "Marks": 400 }
. If another document with _id: 102
already exists, this operation will result in a duplicate key error.
The insert
method in MongoDB is used to add documents to a collection. Here’s how it behaves based on the aspects of Write Concern, Create Collection and the _id
field:
Write Concern determines the level of acknowledgment requested from MongoDB for write operations to ensure that the data is written to the desired number of nodes. It can be configured using the w
(write) and j
(journal) options.
insert
method. For example:db.collection.insert(document, { writeConcern: { w: 1, j: true } })
w
: 1
means the operation will be acknowledged only after the primary node confirms the write.j
: true
means the write operation will be acknowledged only after the write has been committed to the journal.In MongoDB, collections are created implicitly when a document is inserted into a non-existent collection.
db.newCollection.insert({ name: "Alice", age: 25 })
createCollection
method before inserting documents.db.createCollection("explicitCollection")
db.explicitCollection.insert({ name: "Bob", age: 30 })
3. _id
Field
The _id
field in MongoDB is the primary key for documents in a collection. It must be unique for each document. If not provided, MongoDB automatically generates an ObjectId
. Custom values can be specified, but duplicates will result in errors.
In MongoDB, the insert()
method plays a crucial role in efficiently adding documents to collections, ensuring that each document has a unique identifier through the _id
field. This method is not only flexible but also powerful, allowing for the insertion of complex documents that can include nested fields and arrays. While the insert()
method has been deprecated in modern MongoDB shells, understanding its functionality is essential for grasping the foundational concepts of document insertion in MongoDB
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