Skip to main content

mongodb/action/
update.rs

1use crate::bson::{Bson, Document};
2
3use crate::{
4    coll::options::{Hint, UpdateModifications, UpdateOptions},
5    collation::Collation,
6    error::Result,
7    operation::Update as Op,
8    options::WriteConcern,
9    results::UpdateResult,
10    ClientSession,
11    Collection,
12};
13
14use super::{action_impl, deeplink, export_doc, option_setters, options_doc, CollRef};
15
16impl<T> Collection<T>
17where
18    T: Send + Sync,
19{
20    /// Updates all documents matching `query` in the collection.
21    ///
22    /// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be
23    /// passed in place of constructing the enum case. See the official MongoDB
24    /// [documentation](https://www.mongodb.com/docs/manual/reference/command/update/#behavior) for more information on specifying updates.
25    ///
26    /// `await` will return d[`Result<UpdateResult>`].
27    #[deeplink]
28    #[options_doc(update)]
29    pub fn update_many(
30        &self,
31        query: Document,
32        update: impl Into<UpdateModifications>,
33    ) -> Update<'_> {
34        Update {
35            coll: CollRef::new(self),
36            query,
37            update: update.into(),
38            multi: true,
39            options: None,
40            session: None,
41        }
42    }
43
44    /// Updates up to one document matching `query` in the collection.
45    ///
46    /// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be
47    /// passed in place of constructing the enum case. See the official MongoDB
48    /// [documentation](https://www.mongodb.com/docs/manual/reference/command/update/#behavior) for more information on specifying updates.
49    ///
50    /// This operation will retry once upon failure if the connection and encountered error support
51    /// retryability. See the documentation
52    /// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
53    /// retryable writes.
54    ///
55    /// `await` will return d[`Result<UpdateResult>`].
56    #[deeplink]
57    #[options_doc(update)]
58    pub fn update_one(
59        &self,
60        query: Document,
61        update: impl Into<UpdateModifications>,
62    ) -> Update<'_> {
63        Update {
64            coll: CollRef::new(self),
65            query,
66            update: update.into(),
67            multi: false,
68            options: None,
69            session: None,
70        }
71    }
72}
73
74#[cfg(feature = "sync")]
75impl<T> crate::sync::Collection<T>
76where
77    T: Send + Sync,
78{
79    /// Updates all documents matching `query` in the collection.
80    ///
81    /// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be
82    /// passed in place of constructing the enum case. See the official MongoDB
83    /// [documentation](https://www.mongodb.com/docs/manual/reference/command/update/#behavior) for more information on specifying updates.
84    ///
85    /// [`run`](Update::run) will return d[`Result<UpdateResult>`].
86    #[deeplink]
87    #[options_doc(update, "run")]
88    pub fn update_many(
89        &self,
90        query: Document,
91        update: impl Into<UpdateModifications>,
92    ) -> Update<'_> {
93        self.async_collection.update_many(query, update)
94    }
95
96    /// Updates up to one document matching `query` in the collection.
97    ///
98    /// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be
99    /// passed in place of constructing the enum case. See the official MongoDB
100    /// [documentation](https://www.mongodb.com/docs/manual/reference/command/update/#behavior) for more information on specifying updates.
101    ///
102    /// This operation will retry once upon failure if the connection and encountered error support
103    /// retryability. See the documentation
104    /// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
105    /// retryable writes.
106    ///
107    /// [`run`](Update::run) will return d[`Result<UpdateResult>`].
108    #[deeplink]
109    #[options_doc(update, "run")]
110    pub fn update_one(
111        &self,
112        query: Document,
113        update: impl Into<UpdateModifications>,
114    ) -> Update<'_> {
115        self.async_collection.update_one(query, update)
116    }
117}
118
119/// Update documents matching a query.  Construct with [`Collection::update_many`] or
120/// [`Collection::update_one`].
121#[must_use]
122pub struct Update<'a> {
123    coll: CollRef<'a>,
124    query: Document,
125    update: UpdateModifications,
126    multi: bool,
127    options: Option<UpdateOptions>,
128    session: Option<&'a mut ClientSession>,
129}
130
131#[option_setters(crate::coll::options::UpdateOptions)]
132#[export_doc(update)]
133impl<'a> Update<'a> {
134    /// Use the provided session when running the operation.
135    pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
136        self.session = Some(value.into());
137        self
138    }
139}
140
141#[action_impl]
142impl<'a> Action for Update<'a> {
143    type Future = UpdateFuture;
144
145    async fn execute(self) -> Result<UpdateResult> {
146        if let UpdateModifications::Document(d) = &self.update {
147            crate::bson_util::update_document_check(d)?;
148        }
149        let op = Op::with_update(
150            self.coll.clone(),
151            self.query,
152            self.update,
153            self.multi,
154            self.options,
155        );
156        self.coll.client().execute_operation(op, self.session).await
157    }
158}