Search code examples
c++mongo-cxx-driver

How to drop index using mongocxx driver


My program is providing a configurable ttl. So I have to check ttl index every time the program starts, if the expire duration isn't correct, I have to re-index it.

I see drop index function in mongoc driver. But it seems there is no such option in mongocxx driver. Am I missing any api or is there any way to access underlying mongoc_collection_t object, so I can call mongoc driver function?


Solution

  • If you want to drop a single index you can use drop_one, either with the index name:

    db["collection"].indexes().drop_one("department_1");
    

    or with a document:

    db["collection"].indexes().drop_one(make_document(kvp("department", 1)));
    

    Be careful because it will throw if the index does not exist.

    To drop all the indexes you can use drop_all:

    db["collection"].indexes().drop_all();
    

    You have more examples at test/index_view.cpp