thedodd/wither

Add `update_many` method

Closed this issue · 3 comments

Hello,

I need to update in a uniform performant way a potentially large number of documents that match a filter.

In Node.js's mongoose I'm used to being able to just supply a filter and update object, and specify that the number of updates that should occur is not limited.

The biggest benefit of this AFAIK is that the filtering and updating occurs completely in the database memory, there is no ping-pong effect.

Without this method in Wither I think I need to load the list of matches and then send N requests right back to the database. Is this correct?

Can we add an updateMany that sits on top of the method in the MongoDB driver?

Thanks.

@wbrickner thanks for anther great issue. You are correct that a model-level update_many does not yet exist in Wither, we should definitely add it. We have the same thing for delete_many, and adding update_many will be dead simple.

Right now, you can still do this directly via the mongo client (which uses this method):

Model::collection(&db).update_many(query_doc, update_doc, options).await?;

Once we add a wrapper for this method in Wither, your code would just look like:

Model::update_many(&db, query_doc, update_doc, options).await?;

Would you be interested in contributing a PR for this?

I figured out how to do it via collection access. I will submit a PR for a few convenience methods like this one.