mongoFile

mongoFile is a Change Type in the Liquibase Pro extension for MongoDB that allows you to specify mongosh statements from an external file in your changesets. In MongoDB changesets, use mongoFile instead of sqlFile. 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 mongoFile Change Type to store mongosh statements in a separate file and call on it from your XML, YAML, and JSON changelogs. Additionally, you can use the mongo Change Type to specify mongosh in your changelog itself.

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
path String Specifies the file path of the mongosh file to load. Required
dbms String

Specifies which database type(s) a changeset is to be used for. See valid database type names on dbms. 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
encoding String

Encoding used in the file specified in the path attribute. Default: UTF-8.

Optional
relativeToChangelogFile Boolean

Specifies whether the file path is relative to the changelog file rather than looked up in the search path. Default: false.

Optional

Examples

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

The mongoFile 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:mongoFile dbms="mongodb" path="scriptFile.txt" relativeToChangelogFile="true"/>

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

</databaseChangeLog>
databaseChangeLog:
  - changeSet:
      id: 1
      author: your.name
      runWith: mongosh
      changes:
        - mongoFile:
            path: "scriptFile.txt"
            dbms: mongodb
            relativeToChangelogFile: "true"
        - rollback:
            mongo:
              mongo: "db.company.drop()"
{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "1",
        "author": "your.name",
        "runWith": "mongosh",
        "changes": [
          {
            "mongoFile": {
              "dbms": "mongodb",
              "path": "scriptFile.txt",
              "relativeToChangelogFile": true
            }
          }
        ],
        "rollback": [
          {
            "mongo": {
              "mongo": "db.company.drop()"
            }
          }
        ]
      }
    }
  ]
}

Database support

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