MongoDB
MongoDB component can be used to insert, update, delete and select data from Mongo database server.
The component supports both JSON and XML data in request as well as response.
Configuration
Attributes
The figure shows the MongoDB properties without enabling the Show Expert Properties check box.
Figure 1: MongoDB Component Configuration CPS
Connection Configuration
Click the Connection Configuration ellipsis button to set up the configuration required to connect to the MonogDB server.
Figure 2: Connection Configuration dialog box
Database Host(s)
A list of primary and secondary (if present) database servers' host and port details in the form of <host>:<port> can be provided here.
Database Name
Name of the database in the server to perform operations on. For operations like Insert, if there is no database with the given name, it will be created by the component.
Username
Login username for the server.
Password
Password associated with the username provided.
Connection Timeout (in ms)
The timeout in milliseconds until a connection is established. A value is 0 means no timeout.
Connection String
Additional Connection String to specify additional connection options. Each connection option is specified as a pair in the following form: name=value.
- The value is case sensitive.
- Separate the options with ampersand (&) character.
For a complete list of connection string options, please refer to the following link:
https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
Query Configuration
Click the Query Configuration ellipsis button to set up the configuration required to add queries.
Figure 3: Query Configuration dialog box
Any of the CRUD operations can be configured by clicking the Add button and selecting the required operation name from the menu. A Query with default Query Name and Query Type is added to the list of queries.
Query Name
Name of the query to be executed. It should not be empty.
Query Type
Displays query type of the query. This field cannot be edited. The operations supported are described in the below table.
Type of Query | Description |
---|---|
INSERT | Inserts data into the database collection |
BULKINSERT | Inserts multiple documents into the database collection |
UPDATE | Modifies existing data in the database collection |
DELETE | Deletes data from the database collection |
SELECT | Retrieves data from the database collection |
Collection
Collection name in the database on which operation is to be performed. For Insert and Update queries, if the collection specified does not exist, a new collection with the same name will be created.
JSON
If set to true, the input is expected in the form of JSON string else it is expected in XML format. This is demonstrated in the following sections.
Add
This button generates a list of query type which can be used for querying to the MongoDB server.
Delete
This button is used to delete a query from the query list. The query selected will be removed from the list.
Delete All
This button deletes all the queries in the query list.
INSERT
Figure 4: Insert Query Configuration
Right-click the empty space in the element-type table to add a key to insert the required field in the document.
Figure 5: Adding a new field to Element-Type table
Figure 6: Context menu on a field in the Element-Type table
Add Child: Adds a node to the parent node. Refer to http://docs.mongodb.org/manual/tutorial/model-tree-structures-with-child-references/ for further details.
Add Key: Adds another node to the table.
Delete: The selected field will be deleted on clicking this button.
Figure 7: Adding children to a parent node in the Element-Type Table
This structure will generate a document of the form {"key1": {"child1":"val", "child2":"val"}, "key2" : "val"}.
Refer to section "Input and output" for further details about the input.
Figure 8: Up-Down buttons to change the order of elements
Up: Moves the selected field above the previous field.
Down: Moves the selected field below the next field.
BULKINSERT
Figure 9: Bulk Insert Query configuration
This operation is used to insert multiple documents into a collection. A JSON array can be specified to list the documents to be inserted.
Refer to the Input and Output section for the sample input and its output. In the Query Configuration window, provide a Collection name, and set JSON to "true" so that JSON array can be specified in the input to insert multiple documents into the specified collection.
UPDATE
Figure 10: Update Query configuration
Fields present in the first element-type-operator are updated if conditions specified in the second table are met.
Update field Operators
Operator | Description |
---|---|
SET | Sets the value of a field in a document. |
UNSET | Removes the specified field from a document. |
INCREMENT | Increment field by a specified value. |
MULTIPLY | Multiplies the value of the field by the specified amount. |
RENAME | Renames a field. |
MINIMUM | Only updates the field if the specified value is less than the existing field value. |
MAXIMUM | Only updates the field if the specified value is greater than the existing field value. |
PUSH | Adds an item to an array. |
CURRENT_DATE | Sets the value of a field to current date, either as a Date or a Timestamp. |
Update Condition field operators
Operator | Description |
---|---|
GREATER_THAN | Selects those documents where the value of the field is greater than (i.e. >) the specified value. |
GREATER_OR_EQUAL | Selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value |
LESSER_THAN | Selects the documents where the value of the field is less than (i.e. <) the specified value |
LESSER_OR_EQUAL | Selects the documents where the value of the field is less than or equal to (i.e. <=) the specified value. |
NOT_EQUAL | Selects the documents where the value of the field is not equal (i.e. !=) to the specified value |
IN | Selects the documents where the value of a field equals any value in the specified array. |
NOT_IN | Selects the documents where:
|
Upsert |
|
Update multiple documents |
|
The element to be updated is to be specified in the Element-Type table and query condition is to be specified in the Update Condition table.
Refer to section "Input and Output" to know about the effects of these configurations on input and output structures.
Equality is considered by default when none of the operators are chosen.
DELETE
Figure 11: Delete Query Configuration
Add a field which is to be removed from the documents and specify its data type.
For further information, go through the "input and output" section.
SELECT
Figure 12: Select Query configuration
The condition for the operation is to be specified in the Select Condition table. Fields to be retrieved should be specified in the Select Fields table.
Select All Fields
Retrieves all the fields in the document which satisfies the query condition.
Select '_id' Field:
_id field is also included in the output along with the retrieved fields.
For further information, go through the "input and output" section.
ARRAYS
We have many types of arrays like String[],Object[],Boolean[],Int[] etc which are to be selected from the cps to specify operations on that particular type.
Int []
Figure 13: Input to insert an array of type int[]
Input schema
Schema for the above insert operation
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Insert>
<ns1:key>123</ns1:key>
<ns1:key>321</ns1:key>
<ns1:key>456</ns1:key>
<ns1:key>789</ns1:key>
</ns1:Insert>
</ns1:MongoRequest>
Output
Array with "key" : [ 123, 321, 456, 789 ] is added into the collection.
Output Type
JSON: Displays the output in the form of a JSON string.
XML: Displays the output in XML format.
Expert Properties
Enable the Show Expert Properties checkbox to configure these properties.
Expert properties are meant for advanced users; use it with caution.
Figure 14: MongoDB CPS with the Expert properties enabled and highlighted
Validate Input
This property determines whether the input message has to be validated against the schema defined on the input ports.
Refer to the Validate Input section in the Common Configurations page.
Error Handling Configuration
The remedial actions to be taken when a particular error occurs can be configured here.
Please refer the Error Handling section in Common Configurations page for documentation.
Pre Processing XSL Configuration
Pre Processing XSL configuration can be used to transform request message before processing it. Click the ellipses button against the property to configure the properties.
Refer to the Pre/Post Processing XSL Configuration section in the Common Configurations page for details regarding Pre Processing XSL configuration and Post Processing XSL configuration (below).
Post Processing XSL Configuration
Post Processing XSL configuration can be used to transform the response message before sending it to the output port.
Process Message Based on Property
The property helps components to skip certain messages from processing.
Refer to the Process Message Based On a Property section in the Common Configurations page.
Target Namespace
Refer to the Target Namespace section under the Common Configurations page.
Threadpool Configuration
This property is used when there is a need to process messages in parallel within the component, still maintaining the sequence from the external perspective.
Refer to the Threadpool Configuration section in the Common Configurations page.
Functional Demonstration
Connect a Feeder component to insert input to the MongoDB component and add a Display component to display the output.
Figure 15: Sample flow of MongoDB
Input and Output
Input for the Insert operation
Insert operation Input Schemas for different JSON values are given below.
JSON set to false
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Insert>
<ns1:name>abc</ns1:name>
</ns1:Insert>
</ns1:MongoRequest>
JSON set to true
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Insert>{name:"abc"}</ns1:Insert>
</ns1:MongoRequest>
Output for the Insert operation
Figure 16: Output for the Insert operation
Input for Bulk Insert operation
JSON set to true
<ns1:BulkInsert>
[
{ "name" : "abc" },
{ "name" : "def" },
{ "name" : "ghi" }
]
</ns1:BulkInsert>
Output for Bulk Insert Operation
This will insert three documents into the collection specified in the Query Configuration window.
Figure 17: Output for the Bulk Insert operation
Input for the Update operation
Update operation Input Schemas for different JSON values are given below.
JSON set to false
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Update>
<ns1:Condition>
<ns1:qty>70</ns1:qty>
</ns1:Condition>
<ns1:Fields>
<ns1:type>juice</ns1:type>
</ns1:Fields>
</ns1:Update>
</ns1:MongoRequest>
JSON set to true
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Update>
<ns1:Condition>{qty:{$gt:70}}</ns1:Condition>
<ns1:Fields>{type:"juice"}</ns1:Fields>
</ns1:Update>
</ns1:MongoRequest>
It modifies the "type" field as juice in the documents for which the value of "qty" is greater than or equal to 70.
Output for the Update operation
Figure 18: Output for the Update operation
Input for the Delete operation
Delete operation Input Schemas for different JSON values are given below.
JSON set to false
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Delete>
<ns1:name>abc</ns1:name>
</ns1:Delete>
</ns1:MongoRequest>
JSON set to true
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Delete>{name:"abc"}</ns1:Delete>
</ns1:MongoRequest>
Output for the Delete operation
Figure 19: Output for the Delete operation
Input for the Select operation
Select operation Input Schemas for different JSON values are given below.
JSON set to false
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Select>
<ns1:qty>70</ns1:qty>
</ns1:Select>
</ns1:MongoRequest>
JSON set to true
<?xml version="1.0" encoding="UTF-8"?>
<ns1:MongoRequest xmlns:ns1="http://www.fiorano.com/MONGODB/MongoDB1">
<ns1:Select>
<ns1:Condition>{qty:{$gt:70}}</ns1:Condition>
<ns1:Fields>{type:" "}</ns1:Fields>
</ns1:Select>
</ns1:MongoRequest>
Displays the "_id" and "type" fields in the documents which have "qty" field greater than 70.
An empty value in the fields section retrieves all the fields in the documents which are satisfying the condition.
Output for the Select operation
Figure 20: Output for Select operation
Points to note
- Select output type XML for Insert, Update and Delete operations and JSON for Select operation for the ease of use.
While inserting fields of 'Date' data type, please ensure that the date is in the following format to avoid exceptions:
CODEyyyy-MM-dd'T'HH:mm:ssZ
Example
- "2018-09-25T17:09:58+0000" is a valid format.
- "2018-09-25T17:09:58" is not a valid format.