Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h2. Camel MongoDB component

*Available as of Camel 2.10*

According to Wikipedia: "NoSQL is a movement promoting a loosely defined class of non-relational data stores that break with a long history of relational databases and ACID guarantees." NoSQL solutions have grown in popularity in the last few years, and major extremely-used sites and services such as Facebook, LinkedIn, Twitter, etc. are known to use them extensively to achieve scalability and agility.

Basically, NoSQL solutions differ from traditional RDBMS (Relational Database Management Systems) in that they don't use SQL as their query language and generally don't offer ACID-like transactional behaviour nor relational data. Instead, they are designed around the concept of flexible data structures and schemas (meaning that the traditional concept of a database table with a fixed schema is dropped), extreme scalability on commodity hardware and blazing-fast processing.

MongoDB is a very popular NoSQL solution and the camel-mongodb component integrates Camel with MongoDB allowing you to interact with MongoDB collections both as a producer (performing operations on the collection) and as a consumer (consuming documents from a MongoDB collection).

MongoDB revolves around the concepts of documents (not as is office documents, but rather hierarchical data defined in JSON/BSON) and collections. This component page will assume you are familiar with them. Otherwise, visit [http://www.mongodb.org/].

h2. URI format

{code}
mongodb:connectionBean?database=databaseName&collection=collectionName&operation=operationName[&moreOptions...]
{code}

h2. Endpoint options

MongoDB endpoints support the following options, depending on whether they are acting like a Producer or as a Consumer (options vary based on the consumer type too).

{div:class=confluenceTableSmall}
|| Name || Default Value || Description || Producer || Tailable Cursor Consumer ||
| {{database}} | none | *Required.* The name of the database to which this endpoint will be bound. All operations will be executed against this database unless dynamicity is enabled and the {{CamelMongoDbDatabase}} header is set. | (/) | (/) |
| {{collection}} | none | *Required.* The name of the collection (within the specified database) to which this endpoint will be bound. All operations will be executed against this database unless dynamicity is enabled and the {{CamelMongoDbDatabase}} header is set. | (/) | (/) |
| {{operation}} | none | *Required for producers.* The id of the operation this endpoint will execute. Pick from the following:
* Query operations: {{findById}}, {{findOneByQuery}}, {{findAll}}, {{count}}
* Write operations: {{insert}}, {{save}}, {{update}}
* Delete operations: {{remove}}
* Other operations: {{getDbStats}}, {{getColStats}} | | (/) | |
| {{createCollection}} | true | Determines whether the collection will be automatically created in the MongoDB database during endpoint initialisation if it doesn't exist already. If this option is {{false}} and the collection doesn't exist, an initialisation exception will be thrown. | (/) |  |
| {{invokeGetLastError}} | false (behaviour may be inherited from connections WriteConcern) | Instructs the MongoDB Java driver to invoke {{getLastError()}} after every call. Default behaviour in version 2.7.2 of the MongoDB Java driver is that only network errors will cause the operation to fail, because the actual operation is executed asynchronously in the MongoDB server without holding up the client - to increase performance. The client can obtain the real result of the operation by explicitly invoking {{getLastError()}} on the {{WriteResult}} object returned or by setting the appropriate {{WriteConcern}}. If the backend operation has not finished yet, the client will block until the result is available. Setting this option to {{true}} will make the endpoint behave synchronously and return an Exception if the underlying operation failed. | (/) | |
| {{writeConcern}} | none (driver's default) | Set a {{WriteConcern}} on the operation out of MongoDB's parameterised values. See [WriteConcern.valueOf(String)|http://api.mongodb.org/java/current/com/mongodb/WriteConcern.html#valueOf(java.lang.String)]. | (/) | |
| {{writeConcernRef}} | none | Sets a custom {{WriteConcern}} that exists in the Registry. Specify the bean name. | (/) | |
| {{readPreference}} | none | Sets a {{ReadPreference}} on the connection. Accepted values: the name of any inner subclass of [ReadPreference|http://api.mongodb.org/java/current/com/mongodb/ReadPreference.html]. For example: {{PrimaryReadPreference}}, {{SecondaryReadPreference}}, {{TaggedReadPreference}}. | (/) |  |
| {{dynamicity}} | false | If set to true, the endpoint will inspect the {{CamelMongoDbDatabase}} and {{CamelMongoDbCollection}} headers of the incoming message, and if any of them exists, the target collection and/or database will be overridden for that particular operation. Set to false by default to avoid triggering the lookup on every Exchange if the feature is not desired. | (/) |  |
| {{persistentTailTracking}} | false | Enables or disables persistent tail tracking for Tailable Cursor consumers. See below for more information. |  | (/) |
| {{persistentId}} | none | *Required if persistent tail tracking is enabled.* The id of this persistent tail tracker, to separate its records from the rest on the tail-tracking collection. |  | (/) |
| {{tailTrackingIncreasingField}} | none | *Required if persistent tail tracking is enabled.* Correlation field in the incoming record which is of increasing nature and will be used to position the tailing cursor every time it is generated. The cursor will be (re)created with a query of type: tailTrackIncreasingField > lastValue (where lastValue is possibly recovered from persistent tail tracking). Can be of type Integer, Date, String, etc. NOTE: No support for dot notation at the current time, so the field should be at the top level of the document. |  | (/) |
| {{cursorRegenerationDelay}} | 1000ms | Establishes how long the endpoint will wait to regenerate the cursor after it has been killed by the MongoDB server (normal behaviour). |  | (/) |
| {{tailTrackDb}} | same as endpoint's | Database on which the persistent tail tracker will store its runtime information. |  | (/) |
| {{tailTrackCollection}} | camelTailTracking | Collection on which the persistent tail tracker will store its runtime information. |  | (/) |
| {{tailTrackField}} | lastTrackingValue | Field in which the persistent tail tracker will store the last tracked value. | | (/) |

h2. MongoDB operations

h3. Query operations

h4. findById

                from("direct:findById")

                    .to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=findById&dynamicity=true")

                    .to("mock:resultFindById");

h4. findOneByQuery



h4. findAll

The {{findAll}} operation returns all documents matching a query, or none at all, in which case all documents contained in the collection are returned.

{code}
from("direct:findAll")
    .to("mongodb:myDb?database=flights&collection=tickets&operation=findAll")
    .to("mock:resultFindAll");
{code}

Paging and efficient retrieval is supported via the following headers:

|| Header key || Quick constant || Description (extracted from MongoDB API doc) || Expected type ||
| {{CamelMongoDbNumToSkip}} | MongoDbConstants.NUM_TO_SKIP | Discards a given number of elements at the beginning of the cursor. | int/Integer |
| {{CamelMongoDbLimit}} | MongoDbConstants.LIMIT | Limits the number of elements returned. | int/Integer |
| {{CamelMongoDbBatchSize}} | MongoDbConstants.BATCH_SIZE | Limits the number of elements returned in one batch. A cursor typically fetches a batch of result objects and store them locally. If batchSize is positive, it represents the size of each batch of objects retrieved. It can be adjusted to optimize performance and limit data transfer. If batchSize is negative, it will limit of number objects returned, that fit within the max batch size limit (usually 4MB), and cursor will be closed. For example if batchSize is -10, then the server will return a maximum of 10 documents and as many as can fit in 4MB, then close the cursor. Note that this feature is different from limit() in that documents must fit within a maximum size, and it removes the need to send a request to close the cursor server-side. The batch size can be changed even after a cursor is iterated, in which case the setting will apply on the next batch retrieval.| int/Integer |

Additionally, you can set a sortBy criteria by putting the relevant {{DBObject}} describing your sorting in the {{CamelMongoDbSortBy}} header, quick constant: {{MongoDbConstants.SORT_BY}}.

| | MongoDbConstants.BATCH_SIZE | | |
| | MongoDbConstants.BATCH_SIZE | | |

h3. Create/update operations


h4. insert


h4. save


h4. update


h3. Delete operations


h4. remove


h3. Other operations


h4. count


h4. getDbStats

getColStats