mongodb/
action.rs

1//! Action builder types.
2
3mod aggregate;
4mod bulk_write;
5mod client_options;
6mod count;
7mod create_collection;
8mod create_index;
9#[cfg(feature = "in-use-encryption")]
10pub mod csfle;
11mod delete;
12mod distinct;
13mod drop;
14mod drop_index;
15mod find;
16mod find_and_modify;
17pub mod gridfs;
18mod insert_many;
19mod insert_one;
20mod list_collections;
21mod list_databases;
22mod list_indexes;
23mod perf;
24mod replace_one;
25mod run_command;
26mod search_index;
27mod session;
28mod shutdown;
29pub(crate) mod transaction;
30mod update;
31mod watch;
32
33use std::{future::IntoFuture, marker::PhantomData, ops::Deref};
34
35use crate::bson::Document;
36
37pub use aggregate::Aggregate;
38pub use bulk_write::BulkWrite;
39pub use client_options::ParseConnectionString;
40pub use count::{CountDocuments, EstimatedDocumentCount};
41pub use create_collection::CreateCollection;
42pub use create_index::CreateIndex;
43pub use delete::Delete;
44pub use distinct::Distinct;
45pub use drop::{DropCollection, DropDatabase};
46pub use drop_index::DropIndex;
47pub use find::{Find, FindOne};
48pub use find_and_modify::{FindOneAndDelete, FindOneAndReplace, FindOneAndUpdate};
49pub use insert_many::InsertMany;
50pub use insert_one::InsertOne;
51pub use list_collections::ListCollections;
52pub use list_databases::ListDatabases;
53pub use list_indexes::ListIndexes;
54pub use perf::WarmConnectionPool;
55pub use replace_one::ReplaceOne;
56pub use run_command::{RunCommand, RunCursorCommand};
57pub use search_index::{CreateSearchIndex, DropSearchIndex, ListSearchIndexes, UpdateSearchIndex};
58pub use session::StartSession;
59pub use shutdown::Shutdown;
60pub use transaction::{AbortTransaction, CommitTransaction, StartTransaction};
61pub use update::Update;
62pub use watch::Watch;
63
64#[allow(missing_docs)]
65pub struct ListSpecifications;
66#[allow(missing_docs)]
67pub struct ListNames;
68
69#[allow(missing_docs)]
70pub struct ImplicitSession;
71#[allow(missing_docs)]
72pub struct ExplicitSession<'a>(&'a mut crate::ClientSession);
73
74#[allow(missing_docs)]
75pub struct Single;
76#[allow(missing_docs)]
77pub struct Multiple;
78
79use mongodb_internal_macros::{export_doc, option_setters, options_doc};
80
81pub(crate) mod private {
82    pub trait Sealed {}
83}
84
85/// A pending action to execute on the server.  The action can be configured via chained methods and
86/// executed via `await` (or `run` if using the sync client).
87pub trait Action: private::Sealed + IntoFuture {
88    /// If the value is `Some`, call the provided function on `self`.  Convenient for chained
89    /// updates with values that need to be set conditionally.  For example:
90    /// ```rust
91    /// # use mongodb::{Client, error::Result, bson::Document};
92    /// use mongodb::action::Action;
93    /// async fn list_my_collections(client: &Client, filter: Option<Document>) -> Result<Vec<String>> {
94    ///     client.database("my_db")
95    ///         .list_collection_names()
96    ///         .optional(filter, |a, f| a.filter(f))
97    ///         .await
98    /// }
99    /// ```
100    fn optional<Value>(self, value: Option<Value>, f: impl FnOnce(Self, Value) -> Self) -> Self
101    where
102        Self: Sized,
103    {
104        match value {
105            Some(value) => f(self, value),
106            None => self,
107        }
108    }
109}
110
111pub(crate) use mongodb_internal_macros::{action_impl, deeplink};
112
113use crate::Collection;
114
115pub(crate) struct CollRef<'a> {
116    inner: Collection<Document>,
117    _ref: PhantomData<&'a ()>,
118}
119
120impl<'a> CollRef<'a> {
121    fn new<T: Send + Sync>(coll: &'a Collection<T>) -> Self {
122        Self {
123            inner: coll.clone_with_type(),
124            _ref: PhantomData,
125        }
126    }
127}
128
129impl Deref for CollRef<'_> {
130    type Target = Collection<Document>;
131
132    fn deref(&self) -> &Self::Target {
133        &self.inner
134    }
135}