thedodd / wither

An ODM for MongoDB built on the official MongoDB Rust driver.
https://docs.rs/wither
Other
324 stars 40 forks source link

Add `update_many` method #79

Closed wbrickner closed 3 years ago

wbrickner commented 3 years ago

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.

thedodd commented 3 years ago

@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?;
thedodd commented 3 years ago

Would you be interested in contributing a PR for this?

wbrickner commented 3 years ago

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