CreateOrUpdateIndex in C# driver


Thank you for your great effort.
Is there any generic method to CreateOrUpdateIndex ?
Create if the index (CreateIndexModel) not exists
Update if the index options changed
Do nothing if nothing changed

From googling I found an old workaround that not working with the latest driver version:

Hi @Mordechai_Ben_Zechar, and welcome to the forum,

As of current version (v2.11), there is no a built-in method as you have described in MongoDB .NET/C# driver. Although there are methods that you should be able to build upon to create the logic.

These methods are:

Having said the above if you’re intending to introduce automatic indexing mechanism, please note that although indexes can improve query performances, indexes may also present some operational considerations. See Operational Considerations for Indexes for more information.

You may also find the following resources useful:


Thank you for your answer.
Are you planned to add a generic function in the upcoming versions?

HI @Mordechai_Ben_Zechar,

Currently there is no plan to do add a wrapper method.

Please note that index options can not be updated in place. Please consider the impact of dropping and re-creating indexes for a production environment. See also Index Build On Populated Collections.


1 Like