Syntax for runCommand for MongoDB

I’m trying to add liquibase to my project. I need to create a collection along with index and shard keys creation:

db.runCommand({customAction: “CreateCollection”, collection: “name”, offerThroughput: 400, shardKey: “partition_key”, indexes: [{key: {_id: 1}}, {key: {partition_key: 1, some_key: 1},unique: true}]});

offerThroughput key is specific for CosmosDB which I’m using in my project.

I tried next options:

<changeSet id="1" author="author">
        <ext:runCommand>
            <ext:command>
                {customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {partition_key: 1, some_key: 1}, name: "partition_key_1_some_key_1", unique: true}]}
            </ext:command>
        </ext:runCommand>
    </changeSet>
<changeSet id="1" author="author">
        <ext:runCommand>
            <ext:command>
<![CDATA[ {customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {partition_key: 1, some_key: 1}, name: "partition_key_1_some_key_1", unique: true}]}]]>
            </ext:command>
        </ext:runCommand>
    </changeSet>

All my attempts failed. I would appreciate it very much if somebody could share the syntax to run such a command on MongoDB.

Can you provide the Liquibase output for us to see?

Hi @YuriL

Could you please share the Liquibase version, the command you tried running and the output you got (the error will be helpful for new inputs).

Thanks!
Rakhi Agrawal

Liquibase version is 4.4.0. The same for the extension.

My pom.xml:

<properties>
        <liquibase.version>4.4.0</liquibase.version>
        <liquibase-maven-plugin.version>4.4.0</liquibase-maven-plugin.version>
        <liquibase-mongodb.version>4.4.0</liquibase-mongodb.version>
</properties>

        <dependency>
            <groupId>org.liquibase.ext</groupId>
            <artifactId>liquibase-mongodb</artifactId>
            <version>${liquibase-mongodb.version}</version>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>${liquibase-maven-plugin.version}</version>
        </dependency>

 <build>
  <plugins>
        <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase-maven-plugin.version}</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.liquibase.ext</groupId>
                        <artifactId>liquibase-mongodb</artifactId>
                        <version>${liquibase-mongodb.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mongodb</groupId>
                        <artifactId>mongodb-driver-sync</artifactId>
                        <version>4.2.3</version>
                        <scope>compile</scope>
                    </dependency>
                </dependencies>
            </plugin>
  </plugins>
 </build>

db.changelog-master.xml:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">

    <include file="create-index_initial.xml" relativeToChangelogFile="true"/>

</databaseChangeLog>

create-index_initial.xml:

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext
        http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <changeSet id="1" author="author">

            <ext:runCommand>
                <ext:command>
                    <![CDATA[ {customAction: "CreateCollection", collection: "name", offerThroughput: 400, shardKey: "partition_key", indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {partition_key: 1, some_key: 1}, name: "partition_key_1_some_key_1", unique: true}]}]]>
                </ext:command>
            </ext:runCommand>

    </changeSet>

</databaseChangeLog>

The output:

Hi @rakhi @daryldoak , I would appreciate it very much if you could take a look at my question. I provided all the info in my last post.

Hi @YuriL ,

Sorry for the delayed response. Could you please confirm if the snapshot of error you have added is the only error you got? Or would you mind setting logLevel=debug and then executing? This will provide us more logs to debug the issue.

Also, as you mentioned :

Can you please confirm, if you remove this key from the changeset (just to test) are you able to run the changeset successfully? Or you are still getting an error?

Thanks!
Rakhi Agrawal