mongoose save vs insert vs create
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Mongoose is a popular Object Data Modeling (ODM) library for MongoDB and Node.js. One of its core features is interacting with MongoDB databases more easily and efficiently. When working with Mongoose, you often encounter methods like save(), insertMany(), and create(). These methods are crucial for manipulating documents in a MongoDB collection but differ in functionality and usage. This article delves into the technical differences and usage examples of each method.
Mongoose save()
Overview
The save() method is an instance method provided by Mongoose to save individual documents to the database. It has two major functionalities:
- Persist new documents in the collection.
- Update existing documents' fields.
Usage
To use save(), you need an instance of a Mongoose model. This is because save() operates on document instances, not the model itself.
Example
Mongoose insertMany()
Overview
insertMany() is a static method on the Mongoose model used to insert multiple documents into a collection at once. It's highly efficient for batch inserts because it sends all documents in a single operation.
Usage
This method is called on the model itself and accepts an array of plain objects or Mongoose documents.
Example
Mongoose create()
Overview
create() is a combination of the instantiation and save() process. It can be thought of as a shortcut method that creates and saves documents in one go.
Usage
create() operates on the model and can handle one or multiple documents.
Example
Key Differences
Here's a table summarizing the key points of save(), insertMany(), and create():
| Method | Operates On | Use Case (Single vs. Multiple) | Instance vs. Model | Triggers Middleware | Atomicity |
save() | Document Instances | Single | Instance | Yes | No |
insertMany() | Model | Multiple | Model | No (bulk) | Yes |
create() | Model | Single or Multiple | Model | Yes | No |
Additional Details
- Atomicity: While
insertMany()is atomic for inserting a batch, neithersave()norcreate()provide atomic guarantees when dealing with multiple document inserts. - Middleware:
save()andcreate()trigger Mongoose middleware (pre, post hooks), whileinsertMany()does not trigger middleware by default unless specified. - Error Handling:
insertMany()offers better error handling for batch operations, allowing you to continue processing even if some documents fail to insert.
By understanding these nuances, developers can make more informed choices about which method to use based on their specific needs and contexts, ensuring both performance and data integrity are maintained.
Related reading
- Mongoose Schema vs Model?
- Mongoose unique field
- Mongoose Unique values in nested array of objects
- MongooseError MongooseServerSelectionError connection monitor to 52.6.250.23727017
- Mongoose's find method with or condition does not work properly
- MongoRepository findByThisAndThat custom Query with multiple parameters
- Mongorestore don''t know what to do with file db/collection.bson, skipping
- mongorestore file X does not have .bson extension

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.