How to delete index with mongoengine

I have a flask API and I am saving the data in MongoDB with the help of mongoengine and now I need to update the index programmatically. I don’t want to end up creating a new index each and every time instead I want to update the index with a particular name.

Please give me a solution.

Thanks in advance.

Hi @Python_Hub! Thanks for your question!

I don’t think it’s documented, but mongoengine’s create_index method takes a kwargs argument that I believe are just passed on to pymongo under the hood. This means if you want to specify the name for your index, you should be able to with a name keyword argument, as documented in the PyMongo docs.

I don’t use mongoengine myself, but I believe it should look something like this:

Model.create_index([("field_one", 1), ("other_field", -1)], name="my_index_name")

Let me know if this helps!