Using Liquibase with MongoDB

MongoDB is a document-oriented NoSQL database. For more information, see MongoDB Documentation.

Supported versions

  • 5.0

Prerequisites

Install drivers

To use Liquibase and MongoDB, you need to have two JAR files: the JDBC driver and the Liquibase MongoDB extension.

Place your JAR file(s) in the liquibase/lib directory. For more information, see Adding and Updating Liquibase Drivers.

If you use Maven, you also need to download the driver JAR file and configure your Maven pom.xml file to use the local copy of the driver. For more information, see Configuring Liquibase Attributes in your Maven POM File. For example:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-jdbc</artifactId>
    <version>2.0.0</version>
</dependency>

Test your connection

For Liquibase and MongoDB to work, you need to:

  1. Ensure your MongoDB database is configured. See Install MongoDB for more information.
  2. Specify the database URL in the Liquibase properties file:
  3. url: mongodb://hostname:27017/myDatabase

    Note: If you are unsure about how to configure the url property, refer to Connection String URI Format.

  4. Create a text file called changelog (.xml, .sql, .json, or .yaml) in your project directory and add a changeset.

  5. <?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"
    	xmlns:pro="http://www.liquibase.org/xml/ns/pro"
    	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/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
    		http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
    
    <changeSet id="1" author="bob">
    	<ext:createCollection collectionName="myCollection">
    		<ext:options>
    		{
    		validator: {
    			$jsonSchema: {
    				bsonType: "object",
    				required: ["name", "address"],
    				properties: {
    					name: {
    					bsonType: "string",
    					description: "The Name"
    					},
    			address: {
    				bsonType: "string",
    				description: "The Address"
    						}
    					}
    				}
    			},
    			validationAction: "warn",
    			validationLevel: "strict"
    			}
    			</ext:options>
    		</ext:createCollection>
    	</changeSet>
    </databaseChangeLog>
  1. Navigate to your project folder in the CLI and run the Liquibase status command to see whether the connection is successful. You can pass arguments in the CLI or keep them in the Liquibase properties file.
  2. liquibase --username=test --password=test --changelog-file=<changelog.xml> status
  3. Inspect the SQL with the update-sql command. Then make changes to your database with the update command.
  4. liquibase --changelog-file=<changelog.xml> update-sql
    liquibase --changelog-file=<changelog.xml> update

From a database UI tool, ensure that your database contains the table you added along with the DATABASECHANGELOG table and DATABASECHANGELOGLOCK table.

Tip: You can use MongoDB Compass to easily view collections in your database. For example, run the commands use myDatabase and db.myCollection.find().

Related links