Question: How does upsert affect MongoDBs performance?


Upsert" in MongoDB refers to an operation that updates a document if it exists, or inserts a new document if it doesn't exist. This functionality is crucial for many applications requiring data to be dynamic and updated efficiently. However, when discussing performance implications, several factors come into play.

Factors Affecting Performance

  1. Index Usage: Using indexes can significantly speed up the search phase of an upsert operation. An upsert operation must first search the collection to see if the document exists. If there are appropriate indexes that match the query part of your upsert command, MongoDB can locate documents more quickly.

  2. Write Concerns: The write concern level specifies the acknowledgment needed before considering the operation successful. Higher levels of write concern can impact the performance of upsert operations, as they require more resources to confirm the operation's success across multiple nodes.

  3. Document Size: Larger documents take more time to insert or update, affecting the overall performance of an upsert operation.

  4. Server Load and Hardware: The overall load on the MongoDB server and the underlying hardware resources (CPU, memory, disk I/O) also significantly affect upsert performance. High loads or inadequate hardware can slow down upsert operations.

  5. Sharding: In sharded clusters, upserts which cause documents to move between shards (due to shard key updates) can be particularly costly in terms of performance.

Best Practices for Optimizing Upsert Performance

  • Use Appropriate Indexes: Ensure queries used in upsert operations are well-indexed. This reduces the search time during the "find" phase of the upsert.

  • Monitor Write Concerns: Choose a suitable write concern level that balances consistency needs against performance requirements.

  • Batch Operations: When possible, batch multiple upsert requests together to reduce overhead. MongoDB supports bulk write operations which can include upserts.

  • Hardware Considerations: Ensure that the database server has adequate resources. SSDs can significantly improve performance over HDDs, especially for write-heavy workloads including upserts.

Code Example

Here's a simple example of performing an upsert operation in MongoDB using the MongoDB driver for Node.js:

// Assuming you have installed the MongoDB driver via npm and have required it in your project const { MongoClient } = require('mongodb'); async function upsertDocument(collection, filter, update) { const options = { upsert: true }; // Enable upsert option const result = await collection.updateOne(filter, update, options); if (result.upsertedCount > 0) { console.log(`One document was inserted with the id ${result.upsertedId._id}`); } else if (result.modifiedCount > 0) { console.log('One document was updated.'); } else { console.log('No changes were made to the collection.'); } } // Example usage const uri = 'your_mongodb_connection_string_here'; const client = new MongoClient(uri); async function run() { try { await client.connect(); const database = client.db('testdb'); const collection = database.collection('testcol'); const filter = { name: 'John Doe' }; const update = { $set: { age: 30 } }; await upsertDocument(collection, filter, update); } finally { await client.close(); } } run().catch(console.dir);

In this example, we attempt to update a document where the name is 'John Doe'. If such a document doesn't exist, MongoDB will insert a new document with the name 'John Doe' and age 30.


While upsert operations in MongoDB offer powerful functionality to ensure your data remains consistent and up-to-date, understanding their performance implications and adopting best practices can help maintain optimal database performance.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.