1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use mongodb::{Collection as MongoCollection, IndexModel, bson::doc, error::Result, results::CreateIndexResult, options::IndexOptions};
pub struct Collection<T> {
pub collection: MongoCollection<T>,
}
impl<T> Collection<T> {
pub async fn create_index(&self, field: &str) -> Result<CreateIndexResult> {
let index = IndexModel::builder()
.keys(doc! { field: 1 })
.build();
self.collection.create_index(index, None).await
}
pub async fn create_unique_index(&self, field: &str) -> Result<CreateIndexResult> {
let options = IndexOptions::builder().unique(true).build();
let index = IndexModel::builder()
.keys(doc! { field: 1 })
.options(options)
.build();
self.collection.create_index(index, None).await
}
}