MongoDB Connector
MongoDB Connector: Connects to a MongoDB and executes one or more database statements passed as an input XML document with adapter-specific structures.
Parameters
| Adapter | Main class of the adapter (do not change!) Possible values: de.softproject.integration.adapter.mongodb.MongoAdapter : Main class (default) |
| ConnectionName | Alias/reference name for the connection; Corresponds to the Possible values: Name of the connection (e.g. #default (default)). |
| ReadPreference | Read-preference mode; this parameter defines how reads are processed Possible values:
Additional information on read-preference modes is also available at:
|
| WriteConcern | Indicates whether a Possible values:
Additional information on Write Concerns is also available at:
|
Status values
1 | The operation was executed successfully. |
0 | The operation was executed successfully, but without any result. |
-1 | The operation failed due to a technical error. |
Configuring the connection
The connection to MongoDB is configured in mongodb-connections.xml inside the X4 installation folder, e.g. X4\Server\X4DB\0\mongodb-connections.xml .
<MongoDB>
<!-- connects to localhost on the default port without authentication -->
<Connection name="#default" db="test"/>
<Connection name="localhost" db="test">
<Server host="localhost" port="27017"/>
</Connection>
</MongoDB>
Explanation:
/MongoDB/Connection: Represents a named connection; May exist multiple times./MongoDB/Connection/@name: Alias/reference name for the connection./MongoDB/Connection/@db: Name of the database to which the connection should be made, i.e. databases must always be created via external tools (e.g. MongoShell)./MongoDB/Connection/Server: Represents a server entry for a connection; may exist multiple times per connection to address a cluster./MongoDB/Connection/Server/@host: IP/DNS name of the server to connect to (default: 127.0.0.1)/MongoDB/Connection/Server/@port: Port of the server to connect to (default: 27017)/MongoDB/Connection/Credential: credentials for a connection; may exist multiple times per connection./MongoDB/Connection/Credential/@user: User name of the login/MongoDB/Connection/Credential/@password: Login password/MongoDB/Connection/Credential/@source: Origin of credentials (usage currently unclear)
Notice:
If a connection has no server elements, it connects against localhost:27017 without authentication data, regardless of whether credential elements exist or not.
Coding of a document structure
The JSON/Mongo documents are translated into their XML equivalent according to simple rules:
- Documents and objects/substructures are represented by
<Object>. The key-value entries of an object are mapped as child elements of theObjectelement. - Arrays are represented by
<Array>. The array entries are mapped as child elements of theArrayelement. - If it is not an array or object, it is represented by
<Value>. - The data type of a Value is recorded in the
typeattribute on theValueelement (see Supported data types). If no data type is specified,Stringis assumed. - The key-value entries of an object or a substructure get the key set as
nameattributes on the element of the value element.
Invariants:
- For each
{in the JSON, anObjectelement is created in the XML. (Exception: the document root is mapped by other elements in commands). - For each
[in the JSON, anArrayelement is created in the XML. - For each
:in the JSON (which is not part of a string literal) anameattribute is created which contains the value before the:as content. - All child elements of an
Objectelement have anameattribute. - Child elements of an
Arrayelement do not have anameattribute Valueelements do not have a child element.Textnodes exist only as children ofValueelements (exception: formatting whitespace).typeattributes exist only onValueelements (but not everyValueelement must have atypeattribute).
Sample
<Object>
<Value name="some" type="String">thing</Value>
</Object>
Object element. In this case, an Object element would always be necessary there.
Supported data types
The adapter supports the following data types:
NullObjectIdBooleanLongIntDoubleStringDateTimeInteger: String is read asBigIntegerand then stored asLong- D
ecimal:String is read asBigDecimaland then stored asDouble Numeric: If the string contains a.it will be read asDecimal, otherwise asIntegerRegEx: Regular expression according to http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.htmlThis
Valueelement also knows theflagsattribute, which is a string of the following characters and sets the corresponding option on the pattern:c: CANON_EQi: CASE_INSENSITIVEm: MULTILINEs: DOTALLu: UNICODE_CASEL: LITERALN: UNIX_LINESU: UNICODE_CHARACTER_CLASSZ: COMMENTS
Input
The MongoDB Adapter expects specific input XML structures. These input XML structures contain a root with several commands underneath. Likewise, the resulttag attribute is also supported.
Input structure
<!-- Ein Root-Element mit beliebigem Namen. -->
<root>
<insert collection="products" resulttag="insertOutput">
<Value name="item" type="String">card</Value>
<Value name="qty" type="Integer">15</Value>
</insert>
<find collection="products" resulttag="findOutput">
<query>
<Value name="item" type="String">card</Value>
</query>
</find>
<!-- Hier können weitere Kommandos angegeben werden. -->
</root>
This is a complete and executable example. All subsequent examples are merely snippets that must be inserted into a complete input structure with an enclosing root element in order to execute.
Supported commands
-
copy(no MongoDB command) -
find -
insert -
remove -
save -
update
Command copy
With copy all child elements of the copy command are copied to the output.
Input
The copy command has the following syntax:
<copy resulttag="output">
<Child1/>
<Child2>
<Grandchild1/>
<Grandchild2/>
</Child2>
</copy>
Output
This command outputs the following XML structure:
<output>
<Child1/>
<Child2>
<Grandchild1/>
<Grandchild2/>
</Child2>
</output>
Command insert
With insert a document is inserted into the collection. The insert element is also the document root.
- Please note that
Batchinsertwithinsertis not supported. - More information about this command can also be found at http://docs.mongodb.org/manual/reference/method/db.collection.insert/#db.collection.insert.
Input
The
insert command has the following syntax:
<insert collection="foo">
<Value name="bar" type="String">baz</Value>
</insert>
Output
This command outputs a
WriteResult
object containing the status of the operation.
Command save
With save a new document is created or an existing document is updated according to the given data. The save element is also the document root.
Input
The
save command has the following syntax:
<save collection="foo">
<Value name="bar" type="String">baz</Value>
</save>
Output
This command outputs a
WriteResult
object containing the status of the operation.
Command update
With update a document in a collection is modified. Depending on the
update
parameter, the method can modify specific fields of an existing document or a complete document.
Input
The
update command has the following syntax:
<update collection="foo">
<query/>
<update upsert="false" multi="false"/>
</update>
- The
queryelement contains the query/selection part of theupdateand is also the document root for the query. - The
updateelement contains the change part of the update, and is also document root for the change. - The
upsertandmultioptions are specified on the innerupdateelement.
Output
This command outputs a
WriteResult object containing the status of the operation.
Command remove
With remove a document is removed from a collection.
Input
The
remove command has the following syntax:
<remove collection="foo">
<query/>
</remove>
- The
queryelement contains the query/selection part of theremoveand is also the document root for the query.
justOne option is not supported by the Java driver.
This command outputs a
WriteResult object containing the status of the operation.
Command find
With find a document is selected from a collection.
Input
The
find command has the following syntax:
<find collection="foo">
<--! Optional elements -->
<query/>
<projection/>
<sort/>
<hint/>
<min/>
<max/>
<special/>
<cursor skip="0" limit="0" batchSize="0"/>
</find>
- The
queryelement contains the query/selection part offindand is also the document root for the query. - The
projectionelement contains the field selection offindand is also document root for the selection.
The other optional elements control the behavior of the result cursor:
sort(Document root), see also http://docs.mongodb.org/manual/reference/method/cursor.sort/#cursor.sorthint(Document root), see also http://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hintmin(Document root), see also http://docs.mongodb.org/manual/reference/method/cursor.min/#cursor.minmax(Document root), see also http://docs.mongodb.org/manual/reference/method/cursor.max/#cursor.maxspecial(quasi-document root), see also http://api.mongodb.org/java/current/com/mongodb/DBCursor.html#addSpecial(java.lang.String,%20java.lang.Object)For each child node in the root of the
specialdocument, theaddSpecialmethod is called.cursor(no body) is only the carrier for the following three attributes:
This command outputs a Cursor pointing to the documents that meet the query criteria.
Samples
The following examples show Mongo expressions and their respective equivalents in XML.
Examples of document structure:
| JSON/Mongo structure | Corresponding XML structure |
|---|---|
CODE
|
XML
|
CODE
|
XML
|
CODE
|
XML
|
CODE
|
XML
|
CODE
|
XML
|
Call examples:
| Mongo call | Corresponding XML structure |
|---|---|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|
CODE
or
CODE
|
CODE
|
CODE
or
CODE
|
CODE
|
CODE
|
CODE
|
CODE
|