mongo

mongo is a Change Type in the Liquibase Pro extension for MongoDB that allows you to specify mongosh statements in your changesets. In MongoDB changesets, use mongo instead of sql. For more information, see Welcome to MongoDB Shell (mongosh).

Uses

The Liquibase Pro extension for MongoDB includes several modeled Change Types from the Liquibase Open Source version. These let you specify a few MongoDB commands using Liquibase XML, JSON, and YAML changelogs.

However, you may want to specify additional or more complex MongoDB commands. You can use the mongo Change Type to directly specify mongosh statements in your XML, JSON, and YAML changelogs. Additionally, you can use the mongoFile Change Type to store your mongosh in a separate file and call on it from your changelog.

Note: If you want your changelog to be a JavaScript file rather than XML, YAML, or JSON, you can either use unformatted mongosh or format your mongosh according to Liquibase conventions to allow for better change tracking. For more information, see Use Native Executors with MongoDB Pro.

Run

To run this Change Type, follow these steps:

  1. Add the Change Type to your changeset, as shown in the examples on this page.
  2. Specify any required attributes. Use the table on this page to see which ones your database requires.
  3. Deploy your changeset by running the update command:
  4. liquibase update

Available attributes

Name Type Description Requirement
mongo String Specifies the mongosh to execute. Required
dbms String

Specifies which database type(s) a changeset is to be used for. See valid database type names on Liquibase Database Tutorials. Separate multiple databases with commas. Specify that a changeset is not applicable to a particular database type by prefixing with !. The keywords all and none are also available.

Optional

Examples

Tip: To use this Change Type, you must specify mongosh as the value for runWith in the changeset.

The mongo Change Type does not exist in formatted mongosh changelogs. However, you can directly specify mongosh in these changelogs. For more information, see Use Native Executors with MongoDB Pro.

The Liquibase MongoDB Pro extension uses a unique mongodb-pro XML namespace and XSD files in the changelog header. However, the ext prefix used with other extensions is backwards-compatible:

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

    <changeSet id="1" author="your.name" runWith="mongosh">
        <mongodb-pro:mongo dbms="mongodb">
            db.createCollection('person')
        </mongodb-pro:mongo>

        <rollback>
            <mongodb-pro:mongo>
                db.person.drop()
            </mongodb-pro:mongo>
        </rollback>
    </changeSet>

</databaseChangeLog>
databaseChangeLog:
  - changeSet:
      id: 1
      author: your.name
      runWith: mongosh
      changes:
        - mongo:
            mongo: "db.createCollection('person')"
            dbms: mongodb
        - rollback:
            mongo:
              mongo: "db.person.drop()"
{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "1",
        "author": "your.name",
        "runWith": "mongosh",
        "changes": [
          {
            "mongo": {
              "dbms": "mongodb",
              "mongo": "db.createCollection('person')"
            }
          }
        ],
        "rollback": [
          {
            "mongo": {
              "mongo": "db.person.drop()"
            }
          }
        ]
      }
    }
  ]
}

Database support

This Change Type is only supported for MongoDB. It does not support auto rollback.