How to use mongeez

Oleksii Iepishkin edited this page Dec 22, 2015 · 35 revisions

Create change logs in one of the two following formats

XML (use .xml file name extension). No xsd or dtd defined yet (work in progress). Just follow this example:

<mongoChangeLog>
    <changeSet changeId="ChangeSet-1" author="mlysaght">
        <script>
            db.organization.insert({
              "Name" : "10Gen", "Location" : "NYC", DateFounded : {"Year":2008, "Month":01, "day":01}});
            db.organization.insert({
              "Name" : "SecondMarket", "Location" : "NYC", DateFounded : {"Year":2004, "Month":5, "day":4}});
        </script>
    </changeSet>
    <changeSet changeId="ChangeSet-2" author="mlysaght">
        <script>
            db.user.insert({ "Name" : "Michael Lysaght"});
        </script>
        <script>
            db.user.insert({ "Name" : "Oleksii Iepishkin"});
        </script>
    </changeSet>
</mongoChangeLog>

JavaScript (use .js file name extension). Detailed explanation is here.

//mongeez formatted javascript
//changeset mlysaght:ChangeSet-1
db.organization.insert({
    "Name" : "10Gen", "Location" : "NYC", DateFounded : {"Year" : 2008, "Month" : 01, "day" :01}
});
db.organization.insert({
    "Name" : "SecondMarket", "Location" : "NYC", DateFounded : {"Year" : 2004, "Month" : 05, "day" :04}
});
//changeset mlysaght:ChangeSet-2
db.user.insert({ "Name" : "Michael Lysaght"});
db.user.insert({ "Name" : "Oleksii Iepishkin"});

Create a mongeez.xml file that include all change logs

<changeFiles>
    <file path="changeset1.xml"/>
    <file path="changeset2.xml"/>
</changeFiles>

You can mix and match XML and JavaScript change log files freely.

Execute the code directly

Mongeez mongeez = new Mongeez();
mongeez.setFile(new ClassPathResource("/path_to/mongeez.xml"));
mongeez.setMongo(new Mongo(host, port));
mongeez.setDbName("yourDbName");
mongeez.process();

Execute the code using Spring integration

<bean id="mongoUri" class="com.mongodb.MongoURI">
    <constructor-arg value="${mongodb.uri}"/>
</bean>

<bean id="mongo" class="com.mongodb.Mongo">
    <constructor-arg ref="mongoUri"/>
</bean>

<bean id="mongeez" class="org.mongeez.MongeezRunner">
    <property name="mongo" ref="mongo"/>
    <property name="executeEnabled" value="${migrate}"/>
    <property name="dbName" value="${db.name}"/>
    <property name="file" value="classpath:com/secondmarket/mongo/mongeez.xml"/>
</bean>

or if you need to do mongo authentication replace mongeez bean definition with

<bean id="mongeez" class="org.mongeez.MongeezRunner">
    <property name="mongo" ref="mongo"/>
    <property name="userName" value="${mongodb.username}"/>
    <property name="password" value="${mongodb.password}"/>
    <property name="executeEnabled" value="${migrate}"/>
    <property name="dbName" value="${db.name}"/>
    <property name="file" value="classpath:com/secondmarket/mongo/mongeez.xml"/>
</bean>

Maven integration

Maven Central URL : http://repo1.maven.org/maven2

<dependency>
    <groupId>org.mongeez</groupId>
    <artifactId>mongeez</artifactId>
    <version>0.9.6</version>
</dependency>

Download mongeez

repo1.maven.org : http://repo1.maven.org/maven2/org/mongeez/mongeez