Model

Derive Macro Model 

Source
#[derive(Model)]
{
    // Attributes available to this derive:
    #[db]
    #[collection]
    #[index]
    #[validate]
    #[default]
    #[document_id_setter_ident]
    #[index_max_retries]
    #[index_max_init_seconds]
}
Expand description

Procedural macro to derive the Model trait for mongodb schema support.

This macro enables automatic implementation of the Model trait, allowing CRUD operations and schema-based mongodb interaction.

§Required Attributes

  • #[db("your_database_name")]: Specifies the database name.
  • #[collection("your_collection_name")]: Specifies the collection name.

§Example

#[derive(Model, Serialize, Deserialize, Debug)]
#[db("test")]
#[collection("users")]
pub struct User {
    #[serde(skip_serializing_if = "Option::is_none")]
    _id: Option<ObjectId>,
    name: String,
    age: i32,
    active: bool,
}

Once derived, you can use methods like .save(), .find(), .update_one(), .delete(), etc., provided by the Model trait.