Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.41 KB

dropping_indexes.md

File metadata and controls

54 lines (39 loc) · 1.41 KB

Dropping Indexes

We can use drop_index to remove an index.

collection.drop_index("Name_1").unwrap();

By default, an index name is the entry name with _1 appended. We can change the index name by options when we create an index.

The complete code is shown below:

use polodb_core::{bson::doc, Database, IndexModel, IndexOptions};

fn main() {
    let db = Database::open_memory().unwrap();
    let collection = db.collection("name_of_the_collection");

    collection
        .create_index(IndexModel {
            keys: doc! {
                "Name": 1,
            },
            options: Some(IndexOptions {
                unique: Some(true),
                ..Default::default()
            }),
        })
        .unwrap();
    
    collection.drop_index("Name_1").unwrap();

    let result = collection.insert_one(doc! {"Name": "Bob"});
    println!("{:?}", result);

    let result = collection.insert_one(doc! {"Name": "Bob"});
    println!("{:?}", result);
}

Output:

Ok(InsertOneResult { inserted_id: ObjectId("66f160eeaf31a48fb03e3710") })
Ok(InsertOneResult { inserted_id: ObjectId("66f160eeaf31a48fb03e3711") })

Without the uniqueness constraint, we can add duplicated data.

➡️ Next: Custom Types

📘 Back: Table of contents