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