@@ -6,12 +6,15 @@ use super::{ClientSession, Cursor, SessionCursor};
6
6
use crate :: {
7
7
bson:: { Bson , Document } ,
8
8
error:: Result ,
9
+ index:: IndexModel ,
9
10
options:: {
10
11
AggregateOptions ,
11
12
CountOptions ,
13
+ CreateIndexOptions ,
12
14
DeleteOptions ,
13
15
DistinctOptions ,
14
16
DropCollectionOptions ,
17
+ DropIndexOptions ,
15
18
EstimatedDocumentCountOptions ,
16
19
FindOneAndDeleteOptions ,
17
20
FindOneAndReplaceOptions ,
@@ -20,14 +23,22 @@ use crate::{
20
23
FindOptions ,
21
24
InsertManyOptions ,
22
25
InsertOneOptions ,
26
+ ListIndexOptions ,
23
27
ReadConcern ,
24
28
ReplaceOptions ,
25
29
SelectionCriteria ,
26
30
UpdateModifications ,
27
31
UpdateOptions ,
28
32
WriteConcern ,
29
33
} ,
30
- results:: { DeleteResult , InsertManyResult , InsertOneResult , UpdateResult } ,
34
+ results:: {
35
+ CreateIndexResult ,
36
+ CreateIndexesResult ,
37
+ DeleteResult ,
38
+ InsertManyResult ,
39
+ InsertOneResult ,
40
+ UpdateResult ,
41
+ } ,
31
42
Collection as AsyncCollection ,
32
43
Namespace ,
33
44
RUNTIME ,
@@ -210,6 +221,52 @@ impl<T> Collection<T> {
210
221
) )
211
222
}
212
223
224
+ /// Creates the given index on this collection.
225
+ pub fn create_index (
226
+ & self ,
227
+ index : IndexModel ,
228
+ options : impl Into < Option < CreateIndexOptions > > ,
229
+ ) -> Result < CreateIndexResult > {
230
+ RUNTIME . block_on ( self . async_collection . create_index ( index, options) )
231
+ }
232
+
233
+ /// Creates the given index on this collection using the provided `ClientSession`.
234
+ pub fn create_index_with_session (
235
+ & self ,
236
+ index : IndexModel ,
237
+ options : impl Into < Option < CreateIndexOptions > > ,
238
+ session : & mut ClientSession ,
239
+ ) -> Result < CreateIndexResult > {
240
+ RUNTIME . block_on ( self . async_collection . create_index_with_session (
241
+ index,
242
+ options,
243
+ & mut session. async_client_session ,
244
+ ) )
245
+ }
246
+
247
+ /// Creates the given indexes on this collection.
248
+ pub fn create_indexes (
249
+ & self ,
250
+ indexes : impl IntoIterator < Item = IndexModel > ,
251
+ options : impl Into < Option < CreateIndexOptions > > ,
252
+ ) -> Result < CreateIndexesResult > {
253
+ RUNTIME . block_on ( self . async_collection . create_indexes ( indexes, options) )
254
+ }
255
+
256
+ /// Creates the given indexes on this collection using the provided `ClientSession`.
257
+ pub fn create_indexes_with_session (
258
+ & self ,
259
+ indexes : impl IntoIterator < Item = IndexModel > ,
260
+ options : impl Into < Option < CreateIndexOptions > > ,
261
+ session : & mut ClientSession ,
262
+ ) -> Result < CreateIndexesResult > {
263
+ RUNTIME . block_on ( self . async_collection . create_indexes_with_session (
264
+ indexes,
265
+ options,
266
+ & mut session. async_client_session ,
267
+ ) )
268
+ }
269
+
213
270
/// Deletes all documents stored in the collection matching `query`.
214
271
pub fn delete_many (
215
272
& self ,
@@ -316,6 +373,86 @@ impl<T> Collection<T> {
316
373
)
317
374
}
318
375
376
+ /// Drops the index specified by `name` from this collection.
377
+ pub fn drop_index (
378
+ & self ,
379
+ name : impl AsRef < str > ,
380
+ options : impl Into < Option < DropIndexOptions > > ,
381
+ ) -> Result < ( ) > {
382
+ RUNTIME . block_on ( self . async_collection . drop_index ( name, options) )
383
+ }
384
+
385
+ /// Drops the index specified by `name` from this collection using the provided `ClientSession`.
386
+ pub fn drop_index_with_session (
387
+ & self ,
388
+ name : impl AsRef < str > ,
389
+ options : impl Into < Option < DropIndexOptions > > ,
390
+ session : & mut ClientSession ,
391
+ ) -> Result < ( ) > {
392
+ RUNTIME . block_on ( self . async_collection . drop_index_with_session (
393
+ name,
394
+ options,
395
+ & mut session. async_client_session ,
396
+ ) )
397
+ }
398
+
399
+ /// Drops all indexes associated with this collection.
400
+ pub fn drop_indexes ( & self , options : impl Into < Option < DropIndexOptions > > ) -> Result < ( ) > {
401
+ RUNTIME . block_on ( self . async_collection . drop_indexes ( options) )
402
+ }
403
+
404
+ /// Drops all indexes associated with this collection using the provided `ClientSession`.
405
+ pub fn drop_indexes_with_session (
406
+ & self ,
407
+ options : impl Into < Option < DropIndexOptions > > ,
408
+ session : & mut ClientSession ,
409
+ ) -> Result < ( ) > {
410
+ RUNTIME . block_on (
411
+ self . async_collection
412
+ . drop_indexes_with_session ( options, & mut session. async_client_session ) ,
413
+ )
414
+ }
415
+
416
+ /// Lists all indexes on this collection.
417
+ pub fn list_indexes (
418
+ & self ,
419
+ options : impl Into < Option < ListIndexOptions > > ,
420
+ ) -> Result < Cursor < IndexModel > > {
421
+ RUNTIME
422
+ . block_on ( self . async_collection . list_indexes ( options) )
423
+ . map ( Cursor :: new)
424
+ }
425
+
426
+ /// Lists all indexes on this collection using the provided `ClientSession`.
427
+ pub fn list_indexes_with_session (
428
+ & self ,
429
+ options : impl Into < Option < ListIndexOptions > > ,
430
+ session : & mut ClientSession ,
431
+ ) -> Result < SessionCursor < IndexModel > > {
432
+ RUNTIME
433
+ . block_on (
434
+ self . async_collection
435
+ . list_indexes_with_session ( options, & mut session. async_client_session ) ,
436
+ )
437
+ . map ( SessionCursor :: new)
438
+ }
439
+
440
+ /// Gets the names of all indexes on the collection.
441
+ pub fn list_index_names ( & self ) -> Result < Vec < String > > {
442
+ RUNTIME . block_on ( self . async_collection . list_index_names ( ) )
443
+ }
444
+
445
+ /// Gets the names of all indexes on the collection using the provided `ClientSession`.
446
+ pub fn list_index_names_with_session (
447
+ & self ,
448
+ session : & mut ClientSession ,
449
+ ) -> Result < Vec < String > > {
450
+ RUNTIME . block_on (
451
+ self . async_collection
452
+ . list_index_names_with_session ( & mut session. async_client_session ) ,
453
+ )
454
+ }
455
+
319
456
/// Updates all documents matching `query` in the collection using the provided `ClientSession`.
320
457
///
321
458
/// Both `Document` and `Vec<Document>` implement `Into<UpdateModifications>`, so either can be
0 commit comments