mongodb/action/
delete.rs

1use bson::{Bson, Document};
2
3use crate::{
4    coll::options::{DeleteOptions, Hint},
5    collation::Collation,
6    error::Result,
7    operation::Delete as Op,
8    options::WriteConcern,
9    results::DeleteResult,
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    /// Deletes up to one document found matching `query`.
21    ///
22    /// This operation will retry once upon failure if the connection and encountered error support
23    /// retryability. See the documentation
24    /// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
25    /// retryable writes.
26    ///
27    /// `await` will return d[`Result<DeleteResult>`].
28    #[deeplink]
29    #[options_doc(delete)]
30    pub fn delete_one(&self, query: Document) -> Delete {
31        Delete {
32            coll: CollRef::new(self),
33            query,
34            options: None,
35            session: None,
36            limit: Some(1),
37        }
38    }
39
40    /// Deletes all documents stored in the collection matching `query`.
41    ///
42    /// `await` will return d[`Result<DeleteResult>`].
43    #[deeplink]
44    #[options_doc(delete)]
45    pub fn delete_many(&self, query: Document) -> Delete {
46        Delete {
47            coll: CollRef::new(self),
48            query,
49            options: None,
50            session: None,
51            limit: None,
52        }
53    }
54}
55
56#[cfg(feature = "sync")]
57impl<T> crate::sync::Collection<T>
58where
59    T: Send + Sync,
60{
61    /// Deletes up to one document found matching `query`.
62    ///
63    /// This operation will retry once upon failure if the connection and encountered error support
64    /// retryability. See the documentation
65    /// [here](https://www.mongodb.com/docs/manual/core/retryable-writes/) for more information on
66    /// retryable writes.
67    ///
68    /// [`run`](Delete::run) will return d[`Result<DeleteResult>`].
69    #[deeplink]
70    #[options_doc(delete, sync)]
71    pub fn delete_one(&self, query: Document) -> Delete {
72        self.async_collection.delete_one(query)
73    }
74
75    /// Deletes all documents stored in the collection matching `query`.
76    ///
77    /// [`run`](Delete::run) will return d[`Result<DeleteResult>`].
78    #[deeplink]
79    #[options_doc(delete, sync)]
80    pub fn delete_many(&self, query: Document) -> Delete {
81        self.async_collection.delete_many(query)
82    }
83}
84
85/// Deletes documents matching a query.  Construct with [`Collection::delete_one`] or
86/// [`Collection::delete_many`].
87#[must_use]
88pub struct Delete<'a> {
89    coll: CollRef<'a>,
90    query: Document,
91    options: Option<DeleteOptions>,
92    session: Option<&'a mut ClientSession>,
93    limit: Option<u32>,
94}
95
96#[option_setters(crate::coll::options::DeleteOptions)]
97#[export_doc(delete)]
98impl<'a> Delete<'a> {
99    /// Use the provided session when running the operation.
100    pub fn session(mut self, value: impl Into<&'a mut ClientSession>) -> Self {
101        self.session = Some(value.into());
102        self
103    }
104}
105
106#[action_impl]
107impl<'a> Action for Delete<'a> {
108    type Future = DeleteFuture;
109
110    async fn execute(mut self) -> Result<DeleteResult> {
111        resolve_write_concern_with_session!(self.coll, self.options, self.session.as_ref())?;
112
113        let op = Op::new(self.coll.namespace(), self.query, self.limit, self.options);
114        self.coll.client().execute_operation(op, self.session).await
115    }
116}