1 unstable release
new 0.1.0 | May 9, 2025 |
---|
#1589 in Database interfaces
605KB
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
~15–26MB
~397K SLoC