Using Liquibase MongoDB Pro with MongoDB Community and Enterprise Server
MongoDB offers several NoSQL database products. This guide describes how to use Liquibase MongoDB Pro with MongoDB Community Server and MongoDB Enterprise Server. You need a Liquibase Pro license key to use it.
This guide does not imply support for third-party clones or emulations of MongoDB. For a tutorial on using Liquibase with Amazon DocumentDB, see Using Liquibase MongoDB Pro with Amazon DocumentDB. This guide also does not describe the community-maintained Liquibase MongoDB extension. For information on that extension, see Contribute Docs: Use Liquibase with MongoDB.
Verified database versions
- 8
- 7
- 6
- 5
For more information on Liquibase Pro and MongoDB version requirements, see Verified database versions.
Deprecated versions
MongoDB 4.4.x is deprecated as of February 29, 2024.
Prerequisites
- Download and Install Liquibase on your machine.
- Ensure Java is installed: Liquibase requires Java to run. If you used the Liquibase Installer, Java is included automatically. Otherwise, you must install Java manually.
- If you use Liquibase Pro, or a Liquibase Pro extension, confirm that you have a valid license key. For more information, see How to Apply Your Liquibase Pro License Key
Download the extension
Download the latest version of the Liquibase MongoDB Pro Extension here.
Configure MongoDB
- Configure User Roles for MongoDB before continuing.
- Download and Install mongosh if it is not already installed on your machine.
- Download Java 11. The MongoDB Pro extension requires it.
Note: mongosh
is mandatory to use MongoDB with Liquibase Pro and it must be accessible to Liquibase. We recommend that mongosh is in the system PATH
environment variable. If it is not, that location of mongosh must be manually specified in the liquibase.mongosh.conf
file.
Tip: Java 11 may already be present on your machine if you used the installer to install Liquibase. We recommend installing Liquibase with Java 11 with the installer asset available on GitHub.
Configure connection
- Ensure your MongoDB database is configured. See Install MongoDB for more information.
-
Ensure your Liquibase Pro license key is specified. For example, in a
liquibase.properties
file (defaults file):licenseKey: zQl8kNZjZgSp9LvqWQFAtGwiHrpg97UyAfQrNSiJQBCDH8FQPdDzANUpIe4Bj3CZA2IXgDBaoYZFvMw0E/s4JcECB3/A6jO+...
-
Specify the database URL in the file (defaults file), along with other properties you want to set a default value for. Liquibase does not parse the URL. You can either specify the full database connection string or specify the URL using your database's standard connection format.
Be sure to change myDatabase to the name of the authenticating database.
url: mongodb://hostname:27017/myDatabase
Note: If you are unsure about how to configure the url
property, refer to Connection String URI Format.
Test connection
- Create a text file called
changelog
(.js
,.yaml
,.json
, or.xml
) in your project directory and add a changeset. - Native MongoDB Shell (
mongosh
) scripts in MongoDB Query Language (MQL): let developers use Liquibase without modifying existing MQL scripts, which may be JavaScript (.js
) files. - Formatted Mongo changelogs (MongoDB Pro 1.3.0+): add Liquibase changeset metadata to your MQL scripts to use features like rollback, contexts, labels, and the
include
andincludeAll
tags. These must be saved as.js
files. - YAML, JSON, and XML modeled changelogs: specify changes for Liquibase to deploy without the need for MQL scripts. However, you can still deploy MQL scripts in YAML, JSON, and XML changelogs by using the
mongo
andmongoFile
Change Types. Using these Change Types requires you to specifymongosh
as the value of therunWith
attribute for allmongo
andmongoFile
changesets. - Navigate to your project folder in the CLI and run the Liquibase
status
command to see whether the connection is successful: - Then execute these changes to your database with the
update
command: - From a database UI tool, ensure that your database contains the
myCollection
object you added along with the DATABASECHANGELOG collection and DATABASECHANGELOGLOCK collection.
You can write Liquibase changelogs in the MongoDB Pro extension in these ways:
Raw mongosh example
The MongoDB Pro extension lets you use MongoDB's native language of MongoDB Query Language (MQL), which you may be storing in JavaScript files, in Liquibase changesets and Change Types. This is possible because MongoDB Shell (mongosh
) is compatible with Liquibase Pro. For more information, see MongoDB: Write Scripts. Example syntax:
db.createCollection('customers');
Tip: The preceding examples show only the mongo
and mongoFile
Change Types for Liquibase Pro. For a list of all Liquibase Pro and Liquibase Open Source Change Types for MongoDB, including Change Types that you can without the mongosh
native executor, see Liquibase Change Types for MongoDB.
Note: You can specify arguments in the CLI or keep them in the Liquibase properties file.
If your connection is successful, you'll see a message like this:
4 changesets have not been applied to <your_connection_url>
Liquibase command 'status' was executed successfully.
liquibase update --changelog-file=<changelog.xml>
If your update
is successful, Liquibase runs each changeset and displays a summary message ending with:
Liquibase: Update has been successful.
Liquibase command 'update' was executed successfully.
Tip: You can use MongoDB Compass to easily view collections in your database. For example, run the commands use myDatabase
and db.myCollection.find()
.
Now you're ready to start deploying database changes with Liquibase!