Mongo Delivery Method Elements

The STEP Mongo Adapter is an outbound integration endpoint delivery option that receives data from a STEP event queue and loads it into a MongoDB database. The MongoDB is often used for website back-end, reporting, and high performance feeds to other back-end systems.

The Mongo adapter converts STEPXML to JSON and keeps a set of 'raw' collections in MongoDB that are in sync with STEP. Additionally, it allows you to add JavaScript triggers to maintain aggregated collections.

The following illustration shows how the Mongo Adapter can be used to feed a Mongo database that is used as the back-end to a website:

By keeping collections of web-ready JSON documents available in MongoDB, the JSON can be passed directly to the browser's JavaScript. This results in the web server having little processing to do, which means that it needs to pass the right collection result directly back to the browser.

The following drawing illustrates the collections involved:

The STEP Mongo Adapter ensures that the raw collections are a one-to-one mirror of data in STEP. The adapter does not embed nodes found through references or child associations. These nodes are stored separately in the raw collections.

It is possible to configure the adapter so that it creates one or more customized, aggregated MongoDBs (databases or collections) where the documents can be tailored to meet the exact usage requirements, like reporting, analysis, and so on.

The JavaScript triggers allow maintaining aggregated collections, for example:

  • List of children, including title and other data necessary in a tree-like view.
  • Information from the target end of references that is needed in a given view.
  • Mapping to a JSON structure that is closer to the website data model.

The following image shows a MongoDB database containing a raw database that is maintained directly by the STEP Mongo Adapter and an aggregated database maintained by JavaScript triggers.

A JavaScript trigger is executed under certain conditions, for example, only for a specific object type. This reduces the amount of checking necessary in the JavaScript code. Once the raw collection is updated and the JavaScript triggers (if configured) have completed, an event is inserted into the events collection. Also, an update is sent to the search engine.

Note: Aggregated collections and raw collections do not need to be in the same MongoDB instance. However, if required, different collections in the aggregate can be combined into their own MongoDB instance.

Default Databases and Collections

The STEP Mongo Adapter maintains a number of MongoDBs, referred to as 'raw databases', and a number of collections in the raw databases, referred to as 'raw collections.' A raw database is created for each context that is exported and the database is named after the context. For example, if the two contexts EN and FR are exported, two raw databases are created, one named EN and one named FR.

It is possible to configure a string that prepend the raw database name when the STEP Mongo Adapter is configured. When data is exported from multiple contexts, a JSON document is created per context, per exported object. Continuing the previous example, two JSON documents are created for each object: one for the object in the EN context and one for the object in the FR context.

A raw collection is only created when the particular STEP data type is exported. So, if no assets are exported from STEP, no asset collection is created. Each raw database contains all or some of the following raw collections:

Raw Collection

Purpose

product

Exported products

asset

Exported assets

classification

Exported classifications

entity

Exported entities

attribute

Exported attribute definitions

data containers

Exported data container definitions

referenceType

Exported reference types

listofvalues

Exported list of values (LOVs)

unit

Exported unit definitions

Inherited values, calculated values, and unit names on values are handled already in the raw collections. Since changes to these affect replicated information, STEP ensures that appropriate updates are made to sync the replicated information.

STEP JSON and Mongo JSON

The STEP Mongo Adapter converts STEPXML input to STEP JSON (JavaScript Object Notation) documents, and then stores the JSON documents in the MongoDB. The STEP JSON schema can be downloaded from your STEP server at: http://[enter step-server]/files/StepSchema.json

Formally, MongoDB documents are BSON documents, which is a binary representation of a JSON document. For more information, refer to the MongoDB documentation on the web.

For an expanded example of the basic conversion using STEP Mongo Adapter, refer to the Mongo Delivery Method Conversion Example documentation.

JavaScript Triggers

STEP business action triggers with JavaScript populate the aggregated MongoDB databases and collections. You can configure one or more business action triggers for the STEP Mongo Adapter. The business action triggers are fired after the STEP Mongo Adapter has populated the raw collections.

During configuration of the STEP Mongo Adapter, the configuration wizard allows you to select multiple JavaScript to run under different conditions by adding preconditions to the business actions. A precondition is, for example, a JavaScript trigger that is only fired for updates to a given type of objects. For more information, refer to the Applies If Tab section of the Editing a Business Rule or Function documentation.

Due to the nature of inheritance and calculated attributes, the trigger may be called even if there are no changes to the underlying raw objects.

The business action triggers are executed once per JSON document sent to the MongoDB database. If two objects are exported from STEP, two JSON documents are sent to the MongoDB database and the business action triggers are executed twice. If the objects are exported in multiple contexts, one JSON document is generated per context, per object. This means that if two products in two contexts are exported, the business action triggers are executed four times.

Business action triggers are executed on the STEP server. Business actions triggers need access to the MongoDB database to fetch data from the raw collections and write or read data to / from the aggregated collections. For the best performance, network latency between the STEP server and the MongoDB server should be minimal.

Important: Place aggregated collections in their own database so that the raw database contains only collections.

Bound Variables

The JavaScript trigger have accesses to information in the Mongo Adapter environment by binding script variables when the JavaScript trigger is defined.

Type

Gives access to

MongoDBContext

  • The current STEP context.
  • The name of the raw MongoDB database.
  • The MongoDB database via the com.mongodb.Mongo object. For more information, refer to: http://api.mongodb.org/java/current/com/mongodb/Mongo.html. The actual Mongo object that is passed can be a wrapper, but it adheres to the interface

JSON context

The JSON object with the data that has been persisted in MongoDB.

For more information about executing JavaScript in business rules, refer to the Business Action: Execute JavaScript section of the Business Rules documentation.

Example JavaScript to Maintain a Collection in a Classification

The following code-snippet illustrates an example JavaScript that maintains a collection containing the list of products (ID and Name) in a classification.

The structure of the collection is:

{_id: "<classification-id>", products : { { id: "<product-id1>", name: "<product-name1>"}, { id: "<product-id2", name: "<product-name2>"}, ... }
function getClassificationIDs(classrefs) {
    var result = new Array();
    if (classrefs != null) {
        for (i = 0; i < classrefs.targets.size(); ++i) {
            result.push(new String(classrefs.targets[i].targetID));
        }
    }
    return result;
}
 
var db = mongoContext.getMongo().getDB('extra');
var collection = db.getCollection('classificationproducts');
 
var classrefs = mongoData.references ? mongoData.references["Web Classifications"] : null;
 
var classificationIDs = getClassificationIDs(classrefs);
 
var deletequery = {"products.id" : mongoData._id, "_id" : { $nin :  getClassificationIDs(classrefs) }};
var adelete = { $pull : { products : { id : mongoData._id } } };
 
collection.update(deletequery, adelete, false, true);
 
var query = {"products.id" : mongoData._id };
var name = mongoData.name;
var update = { $set : { "products.$.name" : name } };
 
collection.update(query, update, false, true);
 
classificationIDs.map(function(item) {
    var query = { _id : item };
    var update = { $addToSet : { products : { id : mongoData._id, name : name } } };
    collection.update(query, update, true, false);
});