Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

383
Vistas
Rust MongoDB set unique fields

How to set a unique field in the mongodb rust implementation? Are you supposed to use 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;
 }

Thanks for any hint!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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!");
}

...so this is my solution but do i have to use the 'builder'?

over 4 years ago · Santiago Trujillo Denunciar

0

To add to DZWG's answer, you may need to add a type to the 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!");
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda