Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagexml
<?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

The following route defined in Spring XML executes the operation dbStats on a collection.In case you are using a 3.x MongoDB instance you have to use the following bean

Code Block
languagexml
titleGet DB stats for specified collection
<route>
  <from uri="direct:start" />
  <!-- using bean 'mongoBean' defined above -->
  <to uri="mongodb:mongoBean?database=${mongodb.database}&amp;collection=${mongodb.collection}&amp;operation=getDbStats" />
  <to uri="direct:result" />
</route>

 

MongoDB operations - producer endpoints

Query operations

findById

...

<?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
languagexml
titleGet DB stats for specified collection
<route>
  <from uri="direct:start" />
  <!-- using bean 'mongoBean' defined above -->
  <to uri="mongodb:mongoBean?database=${mongodb.database}&amp;collection=${mongodb.collection}&amp;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
titleSupports fields filter

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().constant("{ \"name\": \"Raul Kripalani\" }")
    .to("mongodb:to("mongodb:myDb?database=flights&collection=tickets&operation=findOneByQueryfindById")
    .to("mock:resultFindOneByQueryresultFindById");
Tip
titleSupports fields filter

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

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

Tip
titleSupports fields filter

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

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

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.

...

https://github.com/apache/camel/blob/master/components/camel-mongodb3/src/main/docs/mongodb3-component.adoc