Skip to content
Fyodor Kupolov edited this page May 7, 2013 · 11 revisions

Connection declaration

Driver class: org.scriptella.mongodb.Driver
URL: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Runtime dependencies: MongoDB Java Driver

Usage example.

ETL which inserts a single record and then queries the collection and sends results to the text output:

<script connection-id="mongo">
    {
        operation: 'db.collection.save',
        collection: 'test',
        data: {value: '?{etl.date.now()}'}
    }
</script>

<query connection-id="in">
    {
        operation: 'db.collection.find',
        collection: 'users',
        data: {}
    }
    <script connection-id="log">
        Migrating User: user_id=$user_id
    </script>
</query>

Script and query syntax:

Scriptella uses declarative approach for executing MongoDB command. Every query or script element is JSON object representing a single operation or an array of operations in JSON notation. Every operation has the following syntax:

   {
        operation: <OPERATION_NAME> /* e.g. 'db.collection.find' */ ,
        collection: <COLLECTION_NAME> /* optional name of the collection */,
        data: {<OPERATION_ARGUMENTS>}
    }

The "operation" property is required and represents a name of the database command or method. Currently Sriptella driver supports only major operations like db.runCommand, save, update or find. The data property defines method arguments of the corresponding operation.

Variables and properties substitution

Expressions and variables from parent elements can be referenced by using string literals declared as '?variable' or '?{expression}'.

Please note that a variable name or an expression must be enclosed into quotes in order to be parsed by the standard JSON parser. When sending data to the database, the string literal will be replaced with the actual value or the result of expression evaluation.

Properties of each document returned by the element are available to child elements as variables. JEXL expressions can be used for nested properties(e.g. person.name) or arrays. Example:

<query connection-id="mongo">
    {
        operation: 'db.collection.find',
        collection: 'users'
    }
    <script connection-id="out">
         Get the first value of the array property: ${posts[0]}
         User_ID: $_id
    </script>
</query>

Supported operations:

db.runCommand

Run a specified command. See MongoDB documenation for description.

Example:

<!-- Drop test collection -->
<script connection-id="mongo">
    {
        operation: 'db.runCommand',
        data: { drop: 'test' }
    }
</script>

db.collection.find

Selects documents from the collection and executes nested script/query elements for each document returned. The "data" parameter specifies the selection criteria. See MongoDB documenation for description.

Example

<!-- Migrate all users -->
<query connection-id="in">
    {
        operation: 'db.collection.find',
        collection: 'users',
        data: {}
    }
  <script ....   />
</query>

db.collection.save

Updates an existing document or inserts a document depending on the parameter. See MongoDB documenation for description.

Example

    <script connection-id="out">
        {
            operation: 'db.collection.save',
            collection: 'posts',
            data: {
                user_id: '?user_id',
                post_id: '?post_id',
                text: '?text',
                created: '?created',
                comments: []
            }
        }
    </script>

db.collection.update

Modifies an existing document or documents in a collection. See MongoDB documenation for description.

First 2 values in the data property specify the criteria and the document, other parameters control upsert/multi flags. Possible options:

  • queryDoc, updateDoc
  • queryDoc, updateDoc, options
  • queryDoc, updateDoc, upsert, multi

Example

    <script connection-id="out">
        {
            operation: 'db.collection.update',
            collection: 'posts',
            data: [
                <!-- Query.  Use value of post_id column from COMMENTS table -->
                {post_id: '?post_id'},
                <!-- Update. Use value of text column from COMMENTS table -->
                {$push: {comments: '?text'}}
            ]
        }
    </script>

Known Issues and Limitations