Skip to content

Commit b90af45

Browse files
committed
create index on mongo collection
1 parent bf635f6 commit b90af45

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/database.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{unix_time, AlertId, Result};
44
// TODO: Can this be avoided somehow?
55
use bson::{doc, to_bson};
66
use futures::stream::StreamExt;
7+
use mongodb::IndexModel;
78
use mongodb::{
89
options::{FindOneAndUpdateOptions, ReplaceOptions, ReturnDocument},
910
Client, Database as MongoDb,
@@ -41,11 +42,23 @@ struct PendingAlertsEntry(HashMap<AlertId, Alert>);
4142

4243
impl Database {
4344
pub async fn new(config: DatabaseConfig) -> Result<Self> {
44-
Ok(Database {
45-
db: Client::with_uri_str(config.uri)
46-
.await?
47-
.database(&config.name),
48-
})
45+
let db = Client::with_uri_str(config.uri)
46+
.await?
47+
.database(&config.name);
48+
49+
// Create index for fields `id` and `last_notified`.
50+
let index_model = IndexModel::builder()
51+
.keys(doc! {
52+
"id": 1,
53+
"last_notified": 1,
54+
})
55+
.build();
56+
57+
db.collection::<AlertContext>(PENDING)
58+
.create_index(index_model, None)
59+
.await?;
60+
61+
Ok(Database { db })
4962
}
5063
/// Simply checks if a connection could be established to the database.
5164
pub async fn connectivity_check(&self) -> Result<()> {

0 commit comments

Comments
 (0)