Struct mongod::Updates[][src]

pub struct Updates<U: Update> {
    pub set: Option<U>,
    pub unset: Option<U>,
}

Used for complex updates using MongoDB’s update operators.

NOTE

Not all operators are implemented yet…

Examples

Unset the age of a user.

use mongod::bson::Document;
use mongod::{AsFilter, AsUpdate, Comparator, Error, Update, Updates};

#[derive(Bson, Mongo)]
#[mongo(collection="users", field, filter)]
pub struct User {
    pub name: String,
    pub age: u32,
}

#[derive(Default)]
pub struct UserUpdate {
    pub name: Option<String>,
    pub age: Option<u32>,
}

impl Update for UserUpdate {
    fn new() -> Self {
       UserUpdate::default()
    }
    fn into_document(self) -> Result<Document, Error> {
        let mut doc = Document::new();
        if let Some(value) = self.name {
            doc.insert("name", value);
        }
        if let Some(value) = self.age {
            doc.insert("age", value);
        }
        Ok(doc)
    }
}

impl AsUpdate<UserUpdate> for User {
    fn update() -> UserUpdate {
        UserUpdate::default()
    }
    fn into_update(self) -> UserUpdate {
        UserUpdate {
            name: Some(self.name),
            age: Some(self.age),
        }
    }
}

let client = mongod::Client::default();

let mut filter = User::filter();
filter.name = Some(Comparator::Eq("foo".to_owned()));

// NOTE: Unsetting a field that is not optional or doesn't have a default will cause
// deserialisation from the database to fail.
let unset = UserUpdate {
    age: None,
    ..UserUpdate::default()
};
let updates = Updates {
    unset: Some(unset),
    ..Updates::default()
};

let _cursor = client.update::<User, _, _>(filter, updates).await.unwrap();

Fields

set: Option<U>

Sets the value of a field in a document.

unset: Option<U>

Removes the specified field from a document.

Implementations

impl<U: Update> Updates<U>[src]

pub fn into_document(self) -> Result<Document, Error>[src]

Convert Updates into a BSON Document.

Trait Implementations

impl<U: Default + Update> Default for Updates<U>[src]

impl<U: Default + Update> Update for Updates<U>[src]

Auto Trait Implementations

impl<U> RefUnwindSafe for Updates<U> where
    U: RefUnwindSafe

impl<U> Send for Updates<U> where
    U: Send

impl<U> Sync for Updates<U> where
    U: Sync

impl<U> Unpin for Updates<U> where
    U: Unpin

impl<U> UnwindSafe for Updates<U> where
    U: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,