Search code examples
mongodbliquibase

How to drop an index on Liquibase using MongoDB extension


using Liquibase with MongoDB extension, i tried looking up every piece of documentation but i couldn't find any examples of anyone dropping an Index.

https://github.com/liquibase/liquibase-mongodb/tree/main/src/test/resources/liquibase/ext this link haves pretty much all examples but does not have the the dropIndex one.


Solution

  • You can use runCommand option in liquibase mongo extension. The following changeset could do the trick for you:

        <ext:runCommand>
            <ext:command>
                {
                    dropIndexes: "collection_name",
                    index: "index_name"
                }
            </ext:command>
        </ext:runCommand>
    

    This will work if you already have defined index with name.

    In case you want to drop index by keys you can use dropIndex option:

        <ext:dropIndex collectionName="collection_name">
            <ext:keys>
                {
                   "property1": 1,
                   "property2": 1,
                   ...
                }
            </ext:keys>
        </ext:dropIndex>