From 3adf5535eadfb99dc436f6bca434365f9895e56a Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Mon, 4 Oct 2021 01:02:36 -0400 Subject: [PATCH] fix: create index --- src/web.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/web.rs b/src/web.rs index 5fae423..51521fc 100644 --- a/src/web.rs +++ b/src/web.rs @@ -1,8 +1,8 @@ use std::convert::{Infallible, TryFrom}; use anyhow::{Result, Context}; use std::sync::Arc; -use mongodb::{Client as MongoClient, Collection}; -use mongodb::options::UpdateOptions; +use mongodb::{Client as MongoClient, Collection, IndexModel}; +use mongodb::options::{IndexOptions, UpdateOptions}; use mongodb::results::UpdateResult; use tokio_stream::StreamExt; use warp::{Filter, Reply}; @@ -34,10 +34,28 @@ impl State { .await .context("could not create mongodb client")?; - Ok(Arc::new(Self { + let state = Arc::new(Self { config, mongo, - })) + }); + + state.collection() + .create_index( + IndexModel::builder() + .keys(mongodb::bson::doc! { + "listing.id": 1, + "listing.content_id_lower": 1, + }) + .options(IndexOptions::builder() + .unique(true) + .build()) + .build(), + None, + ) + .await + .context("could not create index")?; + + Ok(state) } pub fn collection(&self) -> Collection {