#indexed #mongo-db #collection #index #derive #bson #mongo-indexed

macro mongo_indexed_derive

derive macro to declaratively index mongo collections

3 unstable releases

0.2.2 Jan 13, 2024
0.2.0 Nov 2, 2023
0.1.0 Nov 2, 2023

#27 in #indexed

Download history 21/week @ 2024-01-13 22/week @ 2024-01-20 2/week @ 2024-01-27 8/week @ 2024-02-03 19/week @ 2024-02-10 26/week @ 2024-02-17 47/week @ 2024-02-24 6/week @ 2024-03-02 1/week @ 2024-03-09 6/week @ 2024-03-16 18/week @ 2024-03-23 62/week @ 2024-03-30 52/week @ 2024-04-06 12/week @ 2024-04-13 19/week @ 2024-04-20

146 downloads per month
Used in mongo_indexed

GPL-3.0-or-later

5KB
106 lines

Configure mongo indexing right on your rust structs

Example

use serde::{Serialize, Deserialize};
use mongo_indexed::{derive::MongoIndexed, Indexed};
use mongodb::bson::doc;

#[derive(Serialize, Deserialize, MongoIndexed)]
#[unique_doc_index(doc! { "username": 1, "email": 1 })]
pub struct User {
  #[serde(rename = "_id")]
  pub id: ObjectId,
  
  #[index]
  pub username: String,

  #[index]
  pub email: String,
}

// Use the collection initializer

#[tokio::main]
async fn main() -> anyhow::Result<()> {
  let mongo = ... // init mongodb::Client

  let create_index = true;

  let users = User::collection(&mongo, "db_name", create_index).await?; // mongodb::Collection<User>

  // use the indexed collection...

  Ok(())
}

Dependencies

~305–750KB
~18K SLoC