Configure Engine to use MongoDB

There are times when you may need access to MongoDB. This section details how you can access MongoDB by configuring Engine.

Here are the steps for configuring Engine to use mongoDB:

Configure the Mongo URI

To define the connection between MongoDB and Engine, add the URI in the config file /config/engine/site-config.xml. (This file can be accessed easily from any site created through the out-of-the-box blueprints, by navigating from the Studio dashboard to Site Config > Configuration, and finally picking up the Engine Site Configuration option from the dropdown).

<site>
  <db>
      <uri>mongodb://{host}:{port}/{database}?readPreference=primary&amp;maxPoolSize=50&amp;minPoolSize=5&amp;maxIdleTimeMS=1000&amp;waitQueueMultiple=200&amp;waitQueueTimeoutMS=100&amp;w=1&amp;journal=true</uri>
  </db>
</site>
where:
  • {host} - required, server address to connect to
  • {port} - optional, with a default value of :27020 in Crafter CMS Authoring
  • {database} - optional, name of the database to authenticate if the connection string includes authentication credentials.

For more details on the Connection String URI format, see https://docs.mongodb.com/manual/reference/connection-string/

Create a GMongo client

To access Mongo from Groovy, we’ll use a GMongo client. We’ll need to add some beans in /config/engine/application-context.xml. (This file can be accessed easily from any site created through the out-of-the-box blueprints, by navigating from the Studio dashboard to Site Config > Configuration, and finally picking up the Engine Site Application Context option from the dropdown).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" parent="crafter.properties"/>

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

   <bean id="mongoClient" class="com.gmongo.GMongoClient">
     <constructor-arg ref="mongoUri"/>
   </bean>

</beans>

Use the client from a Groovy script

We can now use the client from a Groovy script. Here’s a simple script that runs a query:

1
2
3
4
5
6
7
8
def mongo = applicationContext.mongoClient
def db = mongo.getDB("{database}")
def result = null
def record = db.{collection}.findOne(_id: "{some id}")
if (record) {
    result = record.name
}
return result
where:
  • {database} - the name of an existing database
  • {collection} - collection name
  • {some id} - id you’re searching for depending on your database

Publish configuration to Delivery

Until this point all changes have been made from Crafter Studio so they will only affect immediately the authoring environment, for a delivery environment you will need to publish the changed files.

This can be done from the Studio site dashboard with the following steps:

  1. Go to Studio’s site dashboard
Studio Site Dashboard

2. Locate My Recent Activity section and change the filter to All to display the configuration files

Studio Site Dashboard - My Recent Activity
  1. Select all configuration files updated in the previous sections
Studio Site Dashboard - My Recent Activity
  1. Click Approve & Publish from the contextual menu
Studio Site Dashboard - Contextual Menu
  1. Click Submit to close the publish dialog
Studio Site Dashboard - Approve & Publish Dialog

Once the files are deployed to the delivery node and the site context is reloaded the new Configuration will take effect.

Delivery specific configurations

If you need to manage different values for the configuration files depending on the environment you can find more detailed information in the Environment Specific Configurations cook book.