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: liquibase update

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

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 will be used for. See valid database type names on dbms. Separate multiple databases with commas. Specify that a changeset does not apply 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

Note: The mongoFile Change Type does not exist in formatted mongosh changelogs. However, you can directly specify mongosh in YAML, JSON, or XML changelogs. For more information, see Use Native Executors with MongoDB Pro.

mongoFile examples

databaseChangeLog:
  - changeSet:
      id: 1
      author: your.name
      runWith: mongosh
      changes:
        - mongoFile:
            path: scriptFile.txt
            dbms: mongodb
            relativeToChangelogFile: 'true'
        - rollback:
            mongo:
              mongo: db.company.drop()