mongodb-ensure-index

Crates.iomongodb-ensure-index
lib.rsmongodb-ensure-index
version0.1.1
created_at2025-05-09 13:55:11.264545+00
updated_at2025-07-09 00:03:44.248156+00
descriptionMongoose-style ensure-index capabilities for the mongodb crate
homepagehttps://megalithic.llc
repositoryhttps://gitlab.com/megalithic-llc/mongodb-ensure-index-rs.git
max_upload_size
id1667023
size9,997,220
David Rauschenbach (drauschenbach)

documentation

README

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

License Arch

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();
Commit count: 0

cargo fmt