MongoDB Connector Example¶
The MongoDB Connector can be used to perform CRUD operations in the local database as well as in MongoDB Atlas (cloud version of MongoDB).
What you'll build¶
This example explains how to use MongoDB Connector to insert and find documents from a MongoDB database.
The sample API given below demonstrates how the MongoDB connector can be used to connect to the MongoDB Server and perform insert many and find operations on it.
-
/insertmany
: The user sends a request payload that includes the connection information, collection name, and the documents to be inserted. This request is sent to WSO2 EI by invoking the MongodbConnector API. This will insert the documents into the MongoDB database. -
/find
: The user sends the request payload containing the connection information, collection name, and the find query. This request is sent to WSO2 EI by invoking the MongodbConnector API. Once the API is invoked, it returns the documents matching the find query.
If you do not want to configure this yourself, you can simply get the project and run it.
Before you begin¶
If you want to connect to MongoDB Atlas, follow the steps mentioned below to get the connection string.
-
In the Clusters view, click Connect for the cluster to which you want to connect.
-
Click Choose a connection method.
-
Click Connect your application.
-
Select Java from the Driver menu.
-
Select the correct driver version from the Version menu.
-
Clear the Include full driver code example check box to get the connection string.
Configure the connector in WSO2 Integration Studio¶
Follow these steps to set up the Integration Project and the Connector Exporter Project.
{!references/connectors/importing-connector-to-integration-studio.md!}
Creating the Integration Logic¶
-
Create a new integration project named
MongodbConnector
. Be sure to enable a Connector Exporter. -
Right-click the created Integration Project and select, -> New -> Rest API to create the REST API.
-
Provide the API name as
MongoConnector
and the API context as/mongodbconnector
. -
First, create the
/insertmany
resource. This API resource inserts documents into the MongoDB database.
Right-click on the API Resource and go to the Properties view. Let's use a URL template called/insertmany
as there are two API resources inside a single API. The method isPost
. -
Drag the 'insertMany' operation of the MongoDB Connector to the Design view as shown below.
-
Create a connection from the Properties view by clicking the '+' icon as shown below.
Following values can be provided when connecting to the MongoDB database.
- Connection Name - connectionURI
- Connection Type - URI
- Connection URI - mongodb+srv://server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB
- Database - TestDatabase
-
After the connection is successfully created, you can select the new connection from the 'Connection' menu in the properties view.
-
Next, provide JSON expressions for the following two properties. These expressions will retrieve the respective values from the JSON request payload.
- Collection - json-eval($.collection)
- Documents - json-eval($.documents)
-
Drag the Respond Mediator to the canvas. This returns the response message to the client (after inserting documents) as shown below.
-
Create the next API resource (which is
/find
) by dragging another API resource to the Design view. This API resource will find all the documents matching the find query given by the user. This will also be aPOST
request. -
Drag the find operation of the Email Connector to the Design view as shown below.
-
Select 'connectionURI' as the connection from the 'Connection' menu in the properties view.
-
Next, provide JSON expressions for the following two properties. These expressions will retrieve the respective values from the JSON request payload.
- Collection - json-eval($.collection)
- Query - json-eval($.query)
-
Drag the Respond Mediator to the canvas. This returns the response message to the client (after retrieving documents) as shown below.
-
You can find the complete API XML configuration below. You can go to the source view and copy paste the following config.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/mongodbconnector" name="MongodbConnector" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/insertmany">
<inSequence>
<mongodb.insertMany configKey="connectionURI">
<collection>{json-eval($.collection)}</collection>
<documents>{json-eval($.documents)}</documents>
<ordered>True</ordered>
</mongodb.insertMany>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
<resource methods="POST" uri-template="/find">
<inSequence>
<mongodb.find configKey="connectionURI">
<collection>{json-eval($.collection)}</collection>
<query>{json-eval($.query)}</query>
</mongodb.find>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
{!references/connectors/exporting-artifacts.md!}
Get the project¶
You can download the ZIP file and extract the contents to get the project code.
Deployment¶
Follow these steps to deploy the exported CApp in the Enterprise Integrator Runtime.
{!references/connectors/deploy-capp.md!}
Click here for instructions on removing the iterative mongodb server logs
Add the configuration below to remove the iterative org.mongodb.driver.cluster
server logs;
-
Add the following logger to the
log4j2.properties
file in the<PRODUCT_HOME>/conf
folder.logger.org-mongodb-driver-cluster.name = org.mongodb.driver.cluster logger.org-mongodb-driver-cluster.level = WARN
-
Then, add
org-mongodb-driver-cluster
to the list ofloggers
.
Prerequisite
-
Download the Mongo java driver from here.
-
Add the driver to the
<PRODUCT_HOME>/dropins
folder. -
Restart the server.
Testing¶
Insert Many Operation¶
-
Create a file named
insertmany.json
with the following payload:{ "collection": "TestCollection", "documents": [ { "name": "Jane Doe", "_id": "123" }, { "name": "John Doe", "_id": "1234" }, { "name": "Jane Doe", "_id": "12345" } ] }
-
Invoke the API as shown below using the curl command.
Info
The Curl application can be downloaded from here.
curl -H "Content-Type: application/xml" --request POST --data @insertmany.json http://localhost:8290/mongodbconnector/insertmany
Expected Response : You should get a response as given below and the data will be added to the database.
{ "InsertManyResult": "Successful" }
Find Operation¶
Note
In order to find documents by ObjectId, the find query payload should be in the following format:
{
"query": {
"_id": {
"$oid": "6011b180007ce60ab2ad74a5"
}
}
}
-
Create a file called
find.json
with the following payload.{ "collection": "TestCollection", "query": { "name": "Jane Doe" } }
-
Invoke the API using the curl command shown below.
Info
Curl Application can be downloaded from here.
curl -H "Content-Type: application/xml" --request POST --data @find.json http://localhost:8290/mongodbconnector/find
Expected Response : You should get a response similar to the one given below.
[ { "_id": "123", "name": "Jane Doe" }, { "_id": "12345", "name": "Jane Doe" } ]
What's Next¶
- You can deploy and run your project on Docker or Kubernetes. See the instructions in Running the Micro Integrator on Containers.
- To customize this example for your own scenario, see MongoDB Connector Configuration documentation for all operation details of the connector.