...
Name | Default Value | Description | Producer | Tailable Cursor Consumer | |||
---|---|---|---|---|---|---|---|
| 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 | |||||
| none | Required (Except for getDbStats and command operations). 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 | |||||
| none | Available as of Camel 2.12: An optional single field index or compound index to create when inserting new collections. |
| ||||
| none | Required for producers. The id of the operation this endpoint will execute. Pick from the following:
|
| ||||
| 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 (behaviour may be inherited from connections WriteConcern) | Remove in camel 2.16 Instructs the MongoDB Java driver to invoke WriteConcern should be preferred to detect if write occurs without errors. MongoDB strongly discourage to use this method as it is not reliable. |
| ||||
| none (driver's default) | Set a |
| ||||
| none | Sets a custom |
| ||||
| none | Available as of Camel 2.12.4, 2.13.1 and 2.14.0: Sets a ReadPreference on the connection. Accepted values are those supported by the ReadPreference#valueOf() public API. Currently as of MongoDB-Java-Driver version 2.12.0 the supported values are: |
| ||||
| false | If set to true, the endpoint will inspect the |
| ||||
| false | Available as of Camel 2.10.3 and 2.11: In write operations (save, update, insert, etc.), instead of replacing the body with the WriteResult object returned by MongoDB, keep the input body untouched and place the WriteResult in the | | ||||
outputType | DBObjectList for findAll | Available as of Camel 2.16 : Convert the output of the producer to the selected type : "DBObjectList", "DBObject" or "DBCursor" : | |||||
| false | Enables or disables persistent tail tracking for Tailable Cursor consumers. See below for more information. |
| false | Enables or disables persistent tail tracking for Tailable Cursor consumers. See below for more information. |
| |
| 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. |
| ||||
| 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. |
| ||||
| 1000ms | Establishes how long the endpoint will wait to regenerate the cursor after it has been killed by the MongoDB server (normal behaviour). |
| ||||
| same as endpoint's | Database on which the persistent tail tracker will store its runtime information. |
| ||||
| camelTailTracking | Collection on which the persistent tail tracker will store its runtime information. |
| ||||
| lastTrackingValue | Field in which the persistent tail tracker will store the last tracked value. |
|
Configuration of database in Spring XML
The following Spring XML creates a bean defining the connection to a MongoDB instance.
Code Block | ||
---|---|---|
| ||
Code Block | ||
| ||
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mongoBean" class="com.mongodb.Mongo"> <constructor-arg name="host" value="${mongodb.host}" /> <constructor-arg name="port" value="${mongodb.port}" /> </bean> </beans> |
Sample route
Code Block |
---|
<route>
<from uri="direct:start" />
<!-- using bean 'mongoBean' defined above -->
<to uri="mongodb:mongoBean?database=${mongodb.database}&collection=${mongodb.collection}&operation=getDbStats" />
<to uri="direct:result" />
</route> |
MongoDB operations - producer endpoints
Query operations
findById
...
In case you are using a 3.x MongoDB instance you have to use the following bean
Code Block | ||
---|---|---|
| ||
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www. |
...
springframework.org/ |
...
schema/ |
...
beans/spring-beans.xsd">
<bean id="mongoBean" class="com.mongodb.MongoClient">
<constructor-arg name="host" value="${mongodb.host}" />
<constructor-arg name="port" value="${mongodb.port}" />
</bean>
</beans> |
Sample route
The following route defined in Spring XML executes the operation dbStats on a collection.
Code Block | ||||
---|---|---|---|---|
| ||||
<route>
<from uri="direct:start" />
<!-- using bean 'mongoBean' defined above -->
<to uri="mongodb:mongoBean?database=${mongodb.database}&collection=${mongodb.collection}&operation=getDbStats" />
<to uri="direct:result" />
</route> |
MongoDB operations - producer endpoints
Query operations
findById
This operation retrieves only one element from the collection whose _id field matches the content of the IN message body. The incoming object can be anything that has an equivalent to a BSON type. See http://bsonspec.org/#/specification and http://www.mongodb.org/display/DOCS/Java+Types.
Code Block |
---|
from("direct:findById |
Code Block |
---|
from("direct:findById")
.to("mongodb:myDb?database=flights&collection=tickets&operation=findById")
.to("mock:resultFindById");
|
Tip | ||
---|---|---|
| ||
This operation supports specifying a fields filter. See Specifying a fields filter. |
findOneByQuery
Use this operation to retrieve just one element from the collection that matches a MongoDB query. The query object is extracted from the IN message body, i.e. it should be of type DBObject
or convertible to DBObject
. It can be a JSON String or a Hashmap. See #Type conversions for more info.
Example with no query (returns any object of the collection):
Code Block |
---|
from("direct:findOneByQuery")
.to("mongodb:myDb?database=flights&collection=tickets&operation=findOneByQuery")
.to("mock:resultFindOneByQuery");
|
Example with a query (returns one matching result):
Code Block |
---|
from("direct:findOneByQuery") .setBody().constantto("{ \"name\": \"Raul Kripalani\" }") .to("mongodb:mongodb:myDb?database=flights&collection=tickets&operation=findOneByQueryfindById") .to("mock:resultFindOneByQueryresultFindById"); |
Tip | ||
---|---|---|
| ||
This operation supports specifying a fields filter. See Specifying a fields filter. |
...
findOneByQuery
The findAll
operation returns all documents matching a query, or none at all, in which case all documents contained in the collection are returnedUse this operation to retrieve just one element from the collection that matches a MongoDB query. The query object is extracted from the IN message body, i.e. it should be of type DBObject
or convertible to DBObject
. It can be a JSON String or a Hashmap. See #Type conversions for more info.
Example with no query (returns all any object in of the collection):
Code Block |
---|
from("direct:findAllfindOneByQuery") .to("mongodb:myDb?database=flights&collection=tickets&operation=findAllfindOneByQuery") .to("mock:resultFindAllresultFindOneByQuery"); |
Example with a query (returns all one matching resultsresult):
Code Block |
---|
from("direct:findAllfindOneByQuery") .setBody().constant("{ \"name\": \"Raul Kripalani\" }") .to("mongodb:myDb?database=flights&collection=tickets&operation=findAllfindOneByQuery") .to("mock:resultFindAllresultFindOneByQuery"); |
Paging and efficient retrieval is supported via the following headers:
Header key | Quick constant | Description (extracted from MongoDB API doc) | Expected type |
---|---|---|---|
|
| Discards a given number of elements at the beginning of the cursor. | int/Integer |
|
| Limits the number of elements returned. | int/Integer |
|
| 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
.
The findAll
operation will also return the following OUT headers to enable you to iterate through result pages if you are using paging:
Header key | Quick constant | Description (extracted from MongoDB API doc) | Data type |
---|---|---|---|
|
| Number of objects matching the query. This does not take limit/skip into consideration. | int/Integer |
|
| Number of objects matching the query. This does not take limit/skip into consideration. | int/Integer |
Tip | ||
---|---|---|
| ||
This operation supports specifying a fields filter. See Specifying a fields filter. |
...
Tip | ||
---|---|---|
| ||
This operation supports specifying a fields filter. See Specifying a fields filter. |
findAll
The findAll
operation returns all documents matching a query. If your query is empty, all of the documents stored will match and be returned. The query object is extracted from the IN message body, i.e. it should be of type DBObject
or convertible to DBObject
. It can be a JSON String or a Hashmap. See #Type conversions for more info.
Example with no query (returns all documents in the collection):
Code Block |
---|
from("direct:findAll")
.to("mongodb:myDb?database=flights&collection=tickets&operation=findAll")
.to("mock:resultFindAll");
|
Example with a query (returns all matching documents):
Code Block |
---|
from("direct:findAll")
.setBody().constant("{ \"name\": \"Raul Kripalani\" }")
.to("mongodb:myDb?database=flights&collection=tickets&operation=findAll")
.to("mock:resultFindAll");
|
Paging and efficient retrieval is supported via the following headers:
Header key | Quick constant | Description (extracted from MongoDB API doc) | Expected type |
---|---|---|---|
|
| Discards a given number of elements at the beginning of the cursor. | int/Integer |
|
| Limits the number of elements returned. | int/Integer |
|
| 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 |
You can also "stream" the documents returned from the server into your route by including outputType=DBCursor
(Camel 2.16+) as an endpoint option which may prove simpler than setting the above headers. This hands your Exchange the DBCursor from the Mongo driver, just as if you were executing the findAll()
within the Mongo shell, allowing your route to iterate over the results. By default and without this option, this component will load the documents from the driver's cursor into a List and return this to your route - which may result in a large number of in-memory objects. Remember, with a DBCursor do not ask for the number of documents matched - see the MongoDB documentation site for details.
Additionally, you can set a sortBy criteria by putting the relevant DBObject
describing your sorting in the CamelMongoDbSortBy
header, quick constant: MongoDbConstants.SORT_BY
.
The findAll
operation will also return the following OUT headers to enable you to iterate through result pages if you are using paging:
Header key | Quick constant | Description (extracted from MongoDB API doc) | Data type |
---|---|---|---|
|
| Number of objects matching the query. This does not take limit/skip into consideration. | int/Integer |
|
| Number of objects matching the query. This does not take limit/skip into consideration. | int/Integer |
Tip | ||
---|---|---|
| ||
This operation supports specifying a fields filter. See Specifying a fields filter. |
Anchor | ||||
---|---|---|---|---|
|
count
Returns the total number of objects in a collection, returning a Long as the OUT message body.
The following example will count the number of records in the "dynamicCollectionName" collection. Notice how dynamicity is enabled, and as a result, the operation will not run against the "notableScientists" collection, but against the "dynamicCollectionName" collection.
Code Block |
---|
// from("direct:count").to("mongodb:myDb?database=tickets&collection=flights&operation=count&dynamicity=true");
Long result = template.requestBodyAndHeader("direct:count", "irrelevantBody", MongoDbConstants.COLLECTION, "dynamicCollectionName");
assertTrue("Result is not of type Long", result instanceof Long);
|
From Camel 2.14 onwards you can provide a com.mongodb.DBObject
object in the message body as a query, and operation will return the amount of documents matching this criteria.
Code Block |
---|
DBObject query = ...
Long count = template.requestBodyAndHeader("direct:count", query, MongoDbConstants.COLLECTION, "dynamicCollectionName");
|
...
Specifying a fields filter
...
A header with key CamelMongoDbRecordsAffected
is returned (MongoDbConstants.RECORDS_AFFECTED
constant) with type int
, containing the number of records deleted (copied from WriteResult.getN()
).
Other operations
count
aggregate
Available as of Camel 2.14
Perform a aggregation with the given pipeline contained in the body. Aggregations could be long and heavy operations. Use with care.
Returns the total number of objects in a collection, returning a Long as the OUT message body.
The following example will count the number of records in the "dynamicCollectionName" collection. Notice how dynamicity is enabled, and as a result, the operation will not run against the "notableScientists" collection, but against the "dynamicCollectionName" collection.
Code Block |
---|
// route: from("direct:countaggregate").to("mongodb:myDb?database=ticketsscience&collection=flightsnotableScientists&operation=count&dynamicity=true"); Long result = template.requestBodyAndHeader("direct:count", "irrelevantBody", MongoDbConstants.COLLECTION, "dynamicCollectionName"); assertTrue("Result is not of type Long", result instanceof Long); |
From Camel 2.14 onwards you can provide a com.mongodb.DBObject
object in the message body as a query, and operation will return the amount of documents matching this criteria.
Code Block |
---|
DBObject query = ... Long count = template.requestBodyAndHeader("direct:count", query, MongoDbConstants.COLLECTION, "dynamicCollectionName"); aggregate"); from("direct:aggregate") .setBody().constant("[{ $match : {$or : [{\"scientist\" : \"Darwin\"},{\"scientist\" : \"Einstein\"}]}},{ $group: { _id: \"$scientist\", count: { $sum: 1 }} } ]") .to("mongodb:myDb?database=science&collection=notableScientists&operation=aggregate") .to("mock:resultAggregate"); |
getDbStats
Equivalent of running the db.stats()
command in the MongoDB shell, which displays useful statistic figures about the database.
For example:
...
Code Block |
---|
> db.camelTest.stats(); { "ns" : "test.camelTest", "count" : 100, "size" : 5792, "avgObjSize" : 57.92, "storageSize" : 20480, "numExtents" : 2, "nindexes2, "nindexes" : 1, "lastExtentSize" : 16384, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 18176 }, "lastExtentSizeok" : 16384, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1 } 1 } |
Usage example:
Code Block |
---|
// from("direct:getColStats").to("mongodb:myDb?database=flights&collection=tickets&operation=getColStats");
Object result = template.requestBody("direct:getColStats", "irrelevantBody");
assertTrue("Result is not of type DBObject", result instanceof DBObject);
|
The operation will return a data structure similar to the one displayed in the shell, in the form of a DBObject
in the OUT message body.
command
Available as of Camel 2.15
Run the body as a command on database. Usefull for admin operation as getting host informations, replication or sharding status.
Collection parameter is not use for this operation.Usage example:
Code Block |
---|
// route: from("direct:getColStatscommand").to("mongodb:myDb?database=flights&collection=ticketsscience&operation=getColStatscommand"); ObjectDBObject resultcommandBody = new template.requestBodyBasicDBObject("direct:getColStatshostInfo", "irrelevantBody1"); assertTrue("Result is not of type DBObject", result instanceof DBObject); |
The operation will return a data structure similar to the one displayed in the shell, in the form of a DBObject
in the OUT message body.
Object result = template.requestBody("direct:command", commandBody); |
Dynamic operations
An Exchange can override the endpoint's fixed operation by setting the CamelMongoDbOperation
header, defined by the MongoDbConstants.OPERATION_HEADER
constant.
The values supported are determined by the MongoDbOperation enumeration and match the accepted values for the operation
parameter on the endpoint URI.
...
- MongoDB website
- NoSQL Wikipedia article
- MongoDB Java driver API docs - current version
- Unit tests for more examples of usage
https://github.com/apache/camel/blob/master/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc