2 releases
| 0.1.1 | Jul 9, 2025 |
|---|---|
| 0.1.0 | May 9, 2025 |
#2121 in Database interfaces
926 downloads per month
Used in saas-rs-sdk
630KB
mongodb-ensure-index-rs
Mongoose-style "ensure index" capabilities for the mongodb crate.
Given a desired mongo index definition:
- Create it if it doesn't exist
- Update it when possible if it's changed
- Drop & re-create it in all other cases
Installing
$ cargo add mongodb-ensure-index
Testing
$ make check
Usage
use mongodb::IndexModel;
let index_models = vec![
{
IndexModel::builder()
.keys({
doc! {"deletedAt": 1}
})
.options(Some(
IndexOptions::builder().name(Some("deletedAt_1".to_string())).build(),
))
.build()
},
{
IndexModel::builder()
.keys({
doc! {"email": 1}
})
.options(Some({
// Unique non-null emails
IndexOptions::builder()
.name(Some("email_1".to_string()))
.unique(true)
.partial_filter_expression({
doc! {"email": doc!{"$type": "string"}}
})
.build()
}))
.build()
},
];
mongodb_ensure_index::ensure_indexes(coll, index_models).await.unwrap();
Dependencies
~11–23MB
~323K SLoC