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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! # Mongo
//!
//! The `mongod` crate aims to provide a convenient, higher-level wrapper on top of the official
//! `bson` & `mongodb` crates.
//!
//! It provides the following:
//!
//! - Async and [blocking][blocking] Clients
//! - [Bson][bson] extensions
//! - Strong typing through the use of traits
//!
//! The [`mongod::Client`][client] is asynchronous. For applications that need a synchronous
//! solution, the [`mongod::blocking`][blocking] API can be used.
//!
//! ## Making Collections
//!
//! Defining an example collection using derive.
//!
//! ```
//! # mod wrapper {
//! # use mongod_derive::{Bson, Mongo};
//! #[derive(Bson, Mongo)]
//! #[mongo(collection="users", field, filter, update)]
//! pub struct User {
//!     name: String,
//!     age: Option<u32>,
//!     email: Option<String>,
//! }
//! # }
//! ```
//!
//! ## Making requests
//!
//! This crate is opinionated, here are some examples on how to use it for interaction with
//! mongodb. For more complex interactions see the individual implementations:
//!
//! - [`Delete`](query::Delete): Delete documents from a collection
//! - [`Find`](query::Find): Fetch documents from a collection
//! - [`Insert`](query::Insert): Insert documents into a collection
//! - [`Replace`](query::Replace): Replace documents in a collection
//! - [`Update`](query::Update): Update documents in a collection
//!
//! ### Deleting
//!
//! Deleting a user from the users collection.
//!
//! ```no_run
//! # mod wrapper {
//! # use mongod_derive::{Bson, Mongo};
//! # #[derive(Bson, Mongo)]
//! # #[mongo(collection="users", field, filter, update)]
//! # pub struct User {
//! #     name: String,
//! #     age: Option<u32>,
//! #     email: Option<String>,
//! # }
//! # async fn doc() -> Result<(), mongod::Error> {
//! use mongod::{AsFilter, Comparator};
//!
//! let client = mongod::Client::new();
//!
//! let mut filter = User::filter();
//! filter.name = Some(Comparator::Eq("foo".to_owned()));
//!
//! let deleted = client.delete::<User, _>(Some(filter)).await.unwrap();
//! println!("delete {} documents", deleted);
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ### Fetching
//!
//! Fetching users from the users collection.
//!
//! ```no_run
//! # mod wrapper {
//! # use std::convert::TryFrom;
//! # use mongod_derive::{Bson, Mongo};
//! use bson::Document;
//! # #[derive(Debug, Bson, Mongo)]
//! # #[mongo(collection="users", field, filter, update)]
//! # pub struct User {
//! #     name: String,
//! #     age: Option<u32>,
//! #     email: Option<String>,
//! # }
//! # async fn doc() -> Result<(), mongod::Error> {
//! use futures::stream::StreamExt;
//!
//! use mongod::Collection;
//!
//! let client = mongod::Client::new();
//!
//! let mut cursor = client.find::<User, _>(None).await.unwrap();
//! while let Some(res) = cursor.next().await {
//!     if let Ok((id, user)) = res {
//!         println!("{} - {:?}", id, user);
//!     }
//! }
//! # Ok(())
//! # }
//! # }
//! ```
//!
//!T ### Inserting
//!
//! Inserting a user into the users collection.
//!
//! ```no_run
//! # mod wrapper {
//! # use mongod_derive::{Bson, Mongo};
//! # #[derive(Debug, Bson, Mongo)]
//! # #[mongo(collection="users", field, filter, update)]
//! # pub struct User {
//! #     name: String,
//! #     age: Option<u32>,
//! #     email: Option<String>,
//! # }
//! # async fn doc() -> Result<(), mongod::Error> {
//! let client = mongod::Client::new();
//!
//! let user = User {
//!     name: "foo".to_owned(),
//!     age: None,
//!     email: None,
//! };
//!
//! let result = client.insert(vec![user]).await.unwrap();
//! println!("(index: oid) {:?}", result);
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ### Replacing
//!
//! Replacing a user in the users collection.
//!
//! ```no_run
//! # mod wrapper {
//! # use mongod_derive::{Bson, Mongo};
//! # #[derive(Debug, Bson, Mongo)]
//! # #[mongo(collection="users", field, filter, update)]
//! # pub struct User {
//! #     name: String,
//! #     age: Option<u32>,
//! #     email: Option<String>,
//! # }
//! # async fn doc() -> Result<(), mongod::Error> {
//! use mongod::{AsFilter, Comparator};
//!
//! let client = mongod::Client::new();
//!
//! let mut filter = User::filter();
//! filter.name = Some(Comparator::Eq("foo".to_owned()));
//!
//! let user = User {
//!     name: "foo".to_owned(),
//!     age: Some(0),
//!     email: None,
//! };
//!
//! let oid = client.replace_one(filter, user).await.unwrap();
//! println!("{:?}", oid);
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ### Updating
//!
//! Updating a user in the users collection.
//!
//! ```no_run
//! # mod wrapper {
//! # use mongod_derive::{Bson, Mongo};
//! # #[derive(Debug, Bson, Mongo)]
//! # #[mongo(collection="users", field, filter, update)]
//! # pub struct User {
//! #     name: String,
//! #     age: Option<u32>,
//! #     email: Option<String>,
//! # }
//! # async fn doc() -> Result<(), mongod::Error> {
//! use mongod::{AsFilter, Comparator, AsUpdate};
//!
//! let client = mongod::Client::new();
//!
//! let mut filter = User::filter();
//! filter.name = Some(Comparator::Eq("foo".to_owned()));
//!
//! let mut update = User::update();
//! update.age = Some(Some(0));
//!
//! let updates = mongod::Updates {
//!     set: Some(update),
//!     ..Default::default()
//! };
//!
//! let updated = client.update::<User, _, _>(filter, updates).await.unwrap();
//! println!("updated {} documents", updated);
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ## Optional Features
//!
//! The following are a list of [Cargo Features][cargo-features] that cna be enabled or disabled:
//!
//! - **blocking**: Provides the [blocking][] client API.
//! - **chrono**: Provides the [chrono][chrono] support for the [`ext::bson`][ext-bson].
//! - **derive**: Provides the `derive` macros from the [mongo-derive][derive] crate.
//!
//! [blocking]: ./blocking/index.html
//! [bson]: https://docs.rs/bson
//! [client]: ./struct.Client.html
//! [chrono]: https://docs.rs/chrono
//! [derive]: ../mongod_derive/index.html
//! [ext-bson]: ./ext/bson/index.html
//! [schema]: ./schema/index.html
//! [cargo-features]: https://doc.rust-lang.org/stable/cargo/reference/manifest.html#the-features-section

#![deny(missing_docs)]
#![deny(unused_imports)]

#[macro_use]
pub extern crate bson;
#[allow(unused_imports)] // FIXME: Needed til we add logging
#[macro_use]
extern crate log;
pub extern crate mongodb as db;
#[macro_use]
extern crate serde;

pub use self::collection::Collection;
pub use self::error::{Error, Kind as ErrorKind};
pub use self::field::{AsField, Field};
pub use self::filter::{AsFilter, Comparator, Filter};
pub use self::query::Query;
pub use self::r#async::{Client, ClientBuilder, TypedCursor};
pub use self::sort::{Order, Sort};
pub use self::update::{AsUpdate, Update, Updates};

pub(crate) use error::Result;

mod r#async;
#[cfg(feature = "blocking")]
pub mod blocking;
mod collection;
mod error;
pub mod ext;
mod field;
mod filter;
pub mod query;
mod sort;
mod update;

#[cfg(feature = "mongod-derive")]
#[allow(unused_imports)]
#[macro_use]
extern crate mongod_derive;
#[cfg(feature = "mongod-derive")]
#[doc(hidden)]
pub use mongod_derive::*;