Struct mongod::Updates [−][src]
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,
U: RefUnwindSafe,
impl<U> Send for Updates<U> where
U: Send,
U: Send,
impl<U> Sync for Updates<U> where
U: Sync,
U: Sync,
impl<U> Unpin for Updates<U> where
U: Unpin,
U: Unpin,
impl<U> UnwindSafe for Updates<U> where
U: UnwindSafe,
U: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
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]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,