findAndModifyMany()

I am kind of late to the Mongo train, but we are now moving away from Postgres (Heroku) into Mongo (Atlas).
I find it surprising that findAndModify() only works on a single document. We have many nice use cases where we need to update multiple documents AND get their values (old values before the update) in one atomic operation. Just curious if there is any special reason why this hasn’t been implemented yet.

Is findAndModifyMany() something that is planned? Can we vote somewhere on this feature request?

1 Like

Hello @yaron_levi, welcome to the forum.

We have many nice use cases where we need to update multiple documents AND get their values (old values before the update) in one atomic operation

To perform operation(s) on many documents or multiple operations on a document atomically, you can use transactions.

Is findAndModifyMany() something that is planned? Can we vote somewhere on this feature request?

You can try searching for and logging a request at MongoDB Jira.

New features should be suggested at feedback.mongodb.com - Jira is for bug reports for the most part.

I’m curious though why you specifically want findAndModify to take multiple updates/return multiple documents - why not findAndModify for each? Otherwise how do you sort out which returned document was for which findAndModify? Normally write operations return back a single result document, and only read operations return a cursor with multiple documents.

2 Likes

We have a collection where each document is a “bucket” of a specific time span, from which you can check if there are steps available. We want in an atomic, one call, to get stepsTotal and stepsConverted and set stepsConverted to be stepsTotal (because we’ve just “drunk” all the available steps from those documents). We also set the isHasAvailable to FALSE.

If you want multiple document writes to be atomic you can’t do it with update or any other write outside a transaction. Only single document writes are atomic. Sounds like you’ll need to use a transaction.

Asya

I misused the term “atomic” (-:
It doesn’t need to be atomic, just many documents in one “hit”. Just like updateMany works.

1 Like

You want to save round trips to the server. That’s understandable but it’s not currently supported in this scenario - sorry!

feedback.mongodb.com is the place to ask for this feature!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.