pub trait Collection:
Send
+ Sync
+ Sized {
// Required method
fn name() -> &'static str;
// Provided method
fn collection(db: &Database) -> Collection<Self> { ... }
}Expand description
Trait for types that map to MongoDB collections.
This trait provides methods to get the collection name and obtain a typed
MongoDB collection reference. It is typically derived using the #[derive(Collection)]
macro rather than implemented manually.
§Examples
ⓘ
use mongo_collection::Collection;
use serde::{Serialize, Deserialize};
#[derive(Collection, Serialize, Deserialize, Debug, Clone)]
struct User {
username: String,
}
// Get the collection name
let name = User::name(); // "users"
// Get a typed MongoDB collection
let users = User::collection(&db);Required Methods§
Provided Methods§
Sourcefn collection(db: &Database) -> Collection<Self>
fn collection(db: &Database) -> Collection<Self>
Returns a typed MongoDB collection reference for this type.
§Arguments
db- A reference to the MongoDB database
§Returns
A mongodb::Collection<Self> that can be used for database operations.
§Examples
ⓘ
let users = User::collection(&db);
let user = users.find_one(doc! { "username": "alice" }).await?;Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".