¿Cómo establecer un campo único en la implementación de mongodb rust? ¿Se supone que debes usar create_index()
?
async fn create_id_fields(client: Client) -> Result { let a = doc! {"my_id" => "1"}.unwrap; client .database(DB_NAME) .collection(COLL_NAME_TICKET) .create_index(a, unique = true) .await; }
¡Gracias por cualquier pista!
async fn create_id_fields(client: &Client) { let options = IndexOptions::builder().unique(true).build(); let model = IndexModel::builder() .keys(doc! {"number": 1}) .options(options) .build(); client .database(DB_NAME) .collection::<Raffle>(COLL_NAME_RAFFLE) .create_index(model, None) .await .expect("error creating index!"); }
... así que esta es mi solución, pero ¿tengo que usar el 'constructor'?
Para agregar a la respuesta de DZWG, es posible que deba agregar un tipo al 1
.
async fn create_id_fields(client: &Client) { let options = IndexOptions::builder().unique(true).build(); let model = IndexModel::builder() .keys(doc! {"number": 1u32}) .options(options) .build(); client .database(DB_NAME) .collection::<Raffle>(COLL_NAME_RAFFLE) .create_index(model, None) .await .expect("error creating index!"); }