• Home
  • Jobs
  • Courses
  • Teachers
  • For business
  • Blog
  • ES/EN

0

5
Views
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!

7 days ago ·

Santiago Trujillo

2 answers
Answer question

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'?

7 days ago · Santiago Trujillo Report

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!");
}
7 days ago · Santiago Trujillo Report
Answer question
Remote jobs
Loading

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Startups
Legal
Terms and conditions Privacy policy
© 2022 PeakU Inc. All Rights Reserved.