Using Liquibase with MongoDB

The purpose of this document is to guide you through the process of creating a new Liquibase project with MongoDB. In this tutorial, you will generate an example project and follow the instructions to apply and learn concepts associated with creating new Liquibase projects with MongoDB.

Prerequisites

Tutorial

To create a Liquibase project with MongoDB on your machine, begin with the following steps:

  1. Create a new project folder and name it LiquibaseProj.
  2. In your LiquibaseProj folder, create a new text file and name it dbchangelog.xml.
  3. Open the dbchangelog.xml file and update the changelog file with the following code snippet:
  <?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"
            xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd
              http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
          </databaseChangeLog>
  1. In your LiquibaseProj folder, create a new text file and name it liquibase.properties.
  2. Edit the liquibase.properties file to add the following properties:
  3. changeLogFile: dbchangelog.xml
    url: mongodb://hostname:27017/myDatabase
    username: userName
    password: password

Note: If you are unsure about how to configure the url property, more information about the connection string URI format key can be found here https://docs.mongodb.com/manual/reference/connection-string/.

  1. Add a changeset to the changelogchangeset are uniquely identified by author and id attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In the dbchangelog.xml file add a new collection "myCollection" change set as follows:
  2. <?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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.0.xsd
          http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.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>
  3. Open the command prompt. Navigate to the LiquibaseProj directory.
    Run the following command: liquibase update
  4. From MongoDB Compass Community Tool User Interface, check your database changes under “myDatabase”. You should see a new “myCollection” collection added to the database. For example:
use myDatabase
db.myCollection.find()
ID NAME
NULL NULL

Also, you should see two more collections:

  • DATABASECHANGELOG tracking collection – This collection keeps a record of all the changesets that were deployed. This way, next time when you deploy again, the changesets in the changelog will be compared with the DATABASECHANGELOG tracking collection and only the new changesets that were not found in the DATABASECHANGELOG will be deployed. You will notice that a new data set was created in that table with the changeset information we have just deployed. For this example:

db.databaseChangeLog.find()

ID AUTHOR FILENAME DATEEXECUTED ORDEREXECUTED EXECTYPE MDSUM
1 bob dbchangelog.sql date&time 1 EXECUTED checksumvalue
  • DATABASECHANGELOGLOCK – This collection is used internally by Liquibase to manage access to the changelog collection during deployment.