Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Submitted
Created by WelcomeLend Operations
Created on Jan 5, 2026

Add setOnUpdate operator for Upsert operations

What problem are you trying to solve?

Focus on the what and why of the need you have, not the how you'd like it solved.

We regularly want to upsert documents to mongo, and include a useful createdAt and updatedAt timestamp.

createdAt is easy: we use setOnInsert, ensuring that it is added on first insertion, and never later.

It is much harder to set the updatedAt value only if the other data in the document has changed.

Setting it unconditionally means the updatedAt field is set to "now", even when no data in the record has changed. It also forces mongo to report the record as Updated, which throws off stats.

Setting updatedAt conditionally involves having to retrieve the record, diff locally, and then include the updatedAt if necessary. Or add a complex $cond clause against some hash of the data. Both are hacky workarounds prone to race conditions.

I'd like to do a simple upsert, have mongo handle the insert or update as necessary, and then only set updatedAt if something else changed in the record. Mongo already has the latest state available to it and it is the right place to make a determination.

What would you like to see happen?

Describe the desired outcome or enhancement.

I'd like a setOnUpdate operator, which only applies the provided data to the record if something else changed due to $set or $setOnInsert.

The existence of $setOnInsert makes me hopeful something like this is plausible and possible.

The same constraints (same field not appearing in $set etc) would be very reasonable.

Why is this important to you or your team?

Explain how the request adds value or solves a business need.

We do a lot of data loading, and we want to understand how and when data changes. Having stats like "every record was updated" when really only a handful were prevents us from understanding how well our data loading is working, and amongst other things, how often we are getting new data.

What steps, if any, are you taking today to manage this problem?

We choose simplicity and broken stats, but we're not happy about it.