Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
GitHub - javisst/mongobee: MongoDB data migration tool for Java · GitHub
Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - javisst/mongobee: MongoDB data migration tool for Java · GitHub
Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - javisst/mongobee: MongoDB data migration tool for Java · GitHub
Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' GitHub - javisst/mongobee: MongoDB data migration tool for Java · GitHub
Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - javisst/mongobee: MongoDB data migration tool for Java · GitHub
Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - javisst/mongobee: MongoDB data migration tool for Java · GitHub
Skip to content

Repository files navigation

mongobee

Build StatusCoverity Scan Build StatusMaven CentralLicence

mongobee is a Java tool which helps you to manage changes in your MongoDB and synchronize them with your application. The concept is very similar to other db migration tools such as Liquibase or Flyway but without using XML/JSON/YML files.

The goal is to keep this tool simple and comfortable to use.

mongobee provides new approach for adding changes (change sets) based on Java classes and methods with appropriate annotations.

Getting started

Add a dependency

With Maven

<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.13</version>
</dependency>

With Gradle

compile 'org.javassist:javassist:3.18.2-GA'// workaround for ${javassist.version} placeholder issue*
compile 'com.github.mongobee:mongobee:0.13'

Usage with Spring

You need to instantiate Mongobee object and provide some configuration. If you use Spring can be instantiated as a singleton bean in the Spring context. In this case the migration process will be executed automatically on startup.

@BeanpublicMongobeemongobee(){
Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // the package to be scanned for changesetsreturnrunner;
}

Usage without Spring

Using mongobee without a spring context has similar configuration but you have to remember to run execute() method to start a migration process.

Mongobeerunner = newMongobee("mongodb://YOUR_DB_HOST:27017/DB_NAME");
runner.setDbName("yourDbName"); // host must be set if not set in URIrunner.setChangeLogsScanPackage(
"com.example.yourapp.changelogs"); // package to scan for changesetsrunner.execute(); // ------> starts migration changesets

Above examples provide minimal configuration. Mongobee object provides some other possibilities (setters) to make the tool more flexible:

runner.setChangelogCollectionName(logColName); // default is dbchangelog, collection with applied change setsrunner.setLockCollectionName(lockColName); // default is mongobeelock, collection used during migration processrunner.setEnabled(shouldBeEnabled); // default is true, migration won't start if set to false

MongoDB URI format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]

More about URI

Creating change logs

ChangeLog contains bunch of ChangeSets. ChangeSet is a single task (set of instructions made on a database). In other words ChangeLog is a class annotated with @ChangeLog and containing methods annotated with @ChangeSet.

packagecom.example.yourapp.changelogs;
@ChangeLogpublicclassDatabaseChangelog {
@ChangeSet(order = "001", id = "someChangeId", author = "testAuthor")
publicvoidimportantWorkToDo(DBdb){
// task implementation
}
}

@ChangeLog

Class with change sets must be annotated by @ChangeLog. There can be more than one change log class but in that case order argument should be provided:

@ChangeLog(order = "001")
publicclassDatabaseChangelog {
//...
}

ChangeLogs are sorted alphabetically by order argument and changesets are applied due to this order.

@ChangeSet

Method annotated by @ChangeSet is taken and applied to the database. History of applied change sets is stored in a collection called dbchangelog (by default) in your MongoDB

Annotation parameters:

order - string for sorting change sets in one changelog. Sorting in alphabetical order, ascending. It can be a number, a date etc.

id - name of a change set, must be unique for all change logs in a database

author - author of a change set

runAlways - [optional, default: false] changeset will always be executed but only first execution event will be stored in dbchangelog collection

Defining ChangeSet methods

Method annotated by @ChangeSet can have one of the following definition:

@ChangeSet(order = "001", id = "someChangeWithoutArgs", author = "testAuthor")
publicvoidsomeChange1() {
// method without arguments can do some non-db changes
}
@ChangeSet(order = "002", id = "someChangeWithMongoDatabase", author = "testAuthor")
publicvoidsomeChange2(MongoDatabasedb) {
// type: com.mongodb.client.MongoDatabase : original MongoDB driver v. 3.x, operations allowed by driver are possible// example: MongoCollection<Document> mycollection = db.getCollection("mycollection");
Documentdoc = newDocument("testName", "example").append("test", "1");
mycollection.insertOne(doc);
}
@ChangeSet(order = "003", id = "someChangeWithDb", author = "testAuthor")
publicvoidsomeChange3(DBdb) {
// This is deprecated in mongo-java-driver 3.x, use MongoDatabase instead// type: com.mongodb.DB : original MongoDB driver v. 2.x, operations allowed by driver are possible// example: DBCollectionmycollection = db.getCollection("mycollection");
BasicDBObjectdoc = newBasicDBObject().append("test", "1");
mycollection .insert(doc);
}
@ChangeSet(order = "004", id = "someChangeWithJongo", author = "testAuthor")
publicvoidsomeChange4(Jongojongo) {
// type: org.jongo.Jongo : Jongo driver can be used, used for simpler notation// example:MongoCollectionmycollection = jongo.getCollection("mycollection");
mycollection.insert("{test : 1}");
}
@ChangeSet(order = "005", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate) {
// type: org.springframework.data.mongodb.core.MongoTemplate// Spring Data integration allows using MongoTemplate in the ChangeSet// example:mongoTemplate.save(myEntity);
}
@ChangeSet(order = "006", id = "someChangeWithSpringDataTemplate", author = "testAuthor")
publicvoidsomeChange5(MongoTemplatemongoTemplate, Environmentenvironment) {
// type: org.springframework.data.mongodb.core.MongoTemplate// type: org.springframework.core.env.Environment// Spring Data integration allows using MongoTemplate and Environment in the ChangeSet
}

Using Spring profiles

mongobee accepts Spring's org.springframework.context.annotation.Profile annotation. If a change log or change set class is annotated with @Profile, then it is activated for current application profiles.

Example 1: annotated change set will be invoked for a dev profile

@Profile("dev")
@ChangeSet(author = "testuser", id = "myDevChangest", order = "01")
publicvoiddevEnvOnly(DBdb){
// ...
}

Example 2: all change sets in a changelog will be invoked for a test profile

@ChangeLog(order = "1")
@Profile("test")
publicclassChangelogForTestEnv{
@ChangeSet(author = "testuser", id = "myTestChangest", order = "01")
publicvoidtestingEnvOnly(DBdb){
// ...
} }

Enabling @Profile annotation (option)

To enable the @Profile integration, please inject org.springframework.core.env.Environment to you runner.

@Bean@AutowiredpublicMongobeemongobee(Environmentenvironment) {
Mongobeerunner = newMongobee(uri);
runner.setSpringEnvironment(environment)
//... etc
}

Known issues

Mongo java driver conflicts

mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.

Exception:

com.mongodb.WriteConcernException: { "serverUsed" : "localhost" , "err" : "invalid ns to index" , "code" : 10096 , "n" : 0 , "connectionId" : 955 , "ok" : 1.0}

Workaround:

You can exclude mongo-java-driver from mongobee and use your dependency only. Maven example (pom.xml) below:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>

About

MongoDB data migration tool for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages