Trait wither::model::Model
[−]
[src]
pub trait Model<'a> where
Self: Serialize + Deserialize<'a>, { const COLLECTION_NAME: &'static str; fn id(&self) -> Option<ObjectId>; fn set_id(&mut self, _: ObjectId); fn model_write_concern() -> WriteConcern { ... } fn write_concern_w() -> i32 { ... } fn write_concern_w_timeout() -> i32 { ... } fn write_concern_j() -> bool { ... } fn write_concern_fsync() -> bool { ... } fn count(
db: Database,
filter: Option<Document>,
options: Option<CountOptions>
) -> Result<i64> { ... } fn find(
db: Database,
filter: Option<Document>,
options: Option<FindOptions>
) -> Result<Vec<Self>> { ... } fn delete_many(db: Database, filter: Document) -> Result<()> { ... } fn find_one(
db: Database,
filter: Option<Document>,
options: Option<FindOptions>
) -> Result<Option<Self>> { ... } fn delete(&self, db: Database) -> Result<()> { ... } fn save(&mut self, db: Database, filter: Option<Document>) -> Result<()> { ... } fn update(
self,
db: Database,
update: Document,
opts: Option<FindOneAndUpdateOptions>
) -> Result<Self> { ... } fn instance_from_document(document: Document) -> Result<Self> { ... } fn indexes() -> Vec<IndexModel> { ... } fn migrations() -> Vec<Box<Migration>> { ... } fn sync(db: Database) -> Result<()> { ... } }
Model provides data modeling behaviors for interacting with MongoDB database collections.
Associated Constants
const COLLECTION_NAME: &'static str
The name of the collection where this model's data is stored.
Required Methods
fn id(&self) -> Option<ObjectId>
Get the ID for this model instance.
fn set_id(&mut self, _: ObjectId)
Set the ID for this model.
Provided Methods
fn model_write_concern() -> WriteConcern
The model's write concern for database writes.
The default implementation ensures that all writes block until they are journaled, which ensures that an ObjectId will be returned for the inserted document. For most cases, overriding this implementation should be unnecessary.
fn write_concern_w() -> i32
The write replication settings for this model. Defaults to 1
.
fn write_concern_w_timeout() -> i32
The write concern timeout settings for this model. Defaults to 0
.
fn write_concern_j() -> bool
The write concern journal settings for this model. Defaults to true
.
fn write_concern_fsync() -> bool
The write concern fsync settings for this model. Defaults to false
.
fn count(
db: Database,
filter: Option<Document>,
options: Option<CountOptions>
) -> Result<i64>
db: Database,
filter: Option<Document>,
options: Option<CountOptions>
) -> Result<i64>
Count the number of documents in this model's collection matching the given criteria.
fn find(
db: Database,
filter: Option<Document>,
options: Option<FindOptions>
) -> Result<Vec<Self>>
db: Database,
filter: Option<Document>,
options: Option<FindOptions>
) -> Result<Vec<Self>>
Find all instances of this model matching the given query.
fn delete_many(db: Database, filter: Document) -> Result<()>
Delete any model instances matching the given query.
fn find_one(
db: Database,
filter: Option<Document>,
options: Option<FindOptions>
) -> Result<Option<Self>>
db: Database,
filter: Option<Document>,
options: Option<FindOptions>
) -> Result<Option<Self>>
Find the one model record matching your query, returning a model instance.
fn delete(&self, db: Database) -> Result<()>
Delete this model instance by ID.
fn save(&mut self, db: Database, filter: Option<Document>) -> Result<()>
Save the current model instance.
In order to make this method as flexible as possible, its behavior varies a little based on the input and the state of the instance.
When the instance already has an ID, this method will operate purely based on the instance
ID. If no ID is present, and no filter
has been specified, then an ID will be generated.
If a filter
is specified, and no ID exists for the instance, then the filter will be used
and the first document matching the filter will be replaced by this instance. This is
useful when the model has unique indexes on fields which need to be the target of the save
operation.
fn update(
self,
db: Database,
update: Document,
opts: Option<FindOneAndUpdateOptions>
) -> Result<Self>
self,
db: Database,
update: Document,
opts: Option<FindOneAndUpdateOptions>
) -> Result<Self>
Update the current model instance.
As this method is simply a wrapper around MongoDB's
FindOneAndUpdate
operation, the update
argument must be a valid update document. This operation targets the model,
instance by the instance's ID. If its ID is None
, this method will return an error.
All other aspects of this method's input are passthrough.
This method will consume self
, and will return a new instance of Self
based on the given
return options (ReturnDocument::Before | ReturnDocument:: After
).
In order to provide consistent behavior, this method will also ensure that the operation's write
concern journaling
is set to true
, so that we can receive a complete output document.
If this model instance was never written to the database, this operation will return an error.
fn instance_from_document(document: Document) -> Result<Self>
Attempt to serialize the given bson document into an instance of this model.
fn indexes() -> Vec<IndexModel>
Get the vector of index models for this model.
fn migrations() -> Vec<Box<Migration>>
Get the vector of migration objects for this model.
fn sync(db: Database) -> Result<()>
Synchronize this model with the backend.
This routine should be called once per model, early on at boottime. It will synchronize any indexes defined on this model with the backend & will execute any active migrations against the model's collection.
This routine will destroy any indexes found on this model's collection which are not
defined in the response from Self.indexes()
.