Spring boot Embedded MongoDb data prefilled

5.2k Views Asked by At

I want to know if there are any tools like Flyway which can assist in database initialization / migration for mongodb. Some of the thoughts I had is

  1. I have used flapdoodle as embedded mongo with springboot. This works fine but, I need to manually put data into it.
  2. For Junit tests with mongo db, I use nosqlunit. This works perfectly fine with Fongo(Fake mongo). It has support to read data from a json file and prefill the database with data during startup. But this works only with junit, since this is a JUnit extension.

What I am looking for is a mix of both of the above, an embedded mongo which works not only with JUnit and can prefill data from a given json(similar to V1__init.sql in Flyway)
Is there any such tool ?

3

There are 3 best solutions below

0
pvpkiran On BEST ANSWER

Finally, I developed this simple version of data prefill for Mongo. Here is the code.

https://github.com/pvpkiran/mongoprefill

Given seed data, this Autoconfiguration fills up mongo.

0
lrathod On

You can also use Mongobee for this. If runs your changeset when the application loads.

Maven dependency

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

you will need to create the bean for Mongobee in your context xml file

<bean id="mongobee" class="com.github.mongobee.Mongobee">
<constructor-arg ref="mongo"/>
<property name="dbName" value="${mongo.databaseName}"/>
<property name="enabled" value="true"/>
<property name="changeLogsScanPackage" value="basepackagewherechangesetispresent"/>

Now add changeset class

@ChangeLog(order = "1")
public class DatabaseChangeLog {

 @ChangeSet(order = "101", id = "somelogicalnameforthischangeset", author = "nameofpersonwhodidthischange")
 public void setupSeedData(MongoTemplate mongoTemplate) { 
    // run your datasetup, prefill,migration here.
 }

And just like flyway, it also maintains the schema version table, so that same change set does not ran again in same environment.

2
jchrbrt On

You can import JSON data during your tests with flapdoodle

See the answer to a similar question here: Import JSON file in Mongo db using Spring Data Embedded Mongo