Exposing MongoDB as a Data Service
com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links2' is unknown.

Exposing MongoDB as a Data Service

This tutorial will guide you on how to expose data from a MongoDB datasource as a data service using the ESB profile of WSO2 Enterprise Integrator (WSO2 EI).

Follow the steps given below. Also, see the samples in Data Integration Samples.

Install and start MongoDB 

A MongoDB server v2.4.x or v2.2.x should be already running in the default port.

Create a collection as below in the command shell.

Creating a data service

Now, let's start creating the data service from scratch:

  1. Log in to the management console of ESB profile of WSO2 EI using the following URL on your browser:  "https://localhost:9443/carbon/".

  2. Click Create under the Data Service menu to open the Create Data Service window.

  3. Enter the following data service name.

  4. Leave the default values for the other fields.

  5. Click Next to go to the Datasources screen

Connecting to the datasource

When you get to the Add New Data Source screen, click Add New Datasource to open the corresponding screen.

  1. Start by entering the following values:

  2. You can then specify the connection details to the MongoDB database you set up previously. The fields available for the MongoDB datasource type are as follows:

Creating a query to POST data

Follow the steps given below.

  1. Click Add New Query and enter the following details:

  2. Now let's specify input mappings for the data in the MongoDB database. We will create output mappings for the id and name fields.

    1. Click Add New Input Mapping to start creating the input mapping. We want to add data into the database using the id and name fields. First, create the input mapping for id.

    2. Click Add to save the input mapping.

    3. Create the input mapping for the name field.

  3. Save the query.

Creating a query to GET data

Now, let's start writing a query for getting data from the MongoDB datasource. The query will specify the data that should be fetched by this query, and the format that should be used to display data when the query is invoked.

  1. Click Add New Query, and enter the following data:

  2. Define Output Mapping: Now, let's specify how the data fetched from the datasource should be displayed in the output. We will create output mappings for the Data field.

    1. Start by giving the following information:

    2. Click Add New Output Mapping to start creating the output mapping. Enter the following values:

    3. Click Main Configuration to return to the Query screen.

  3. Save the query, and click Next to go to the Operations screen.

Creating SOAP operations to invoke the queries

To invoke the query, you need to define an operation.

  1. Click Add New Operation and enter the following information.

  2. Save the operation.

  3. Click Add New Operation and enter the following information.

  4. Save the operation.

You can now invoke the data service query using SOAP.

Creating REST resources to invoke the queries

Now, let's create REST resources to invoke the query created above. Alternatively, you can create SOAP operations to invoke the queries. See the previous section for instructions.

  1. Click Add New Resource and enter the following information.

  2. Save the resource.

  3. Click Add New Resource and enter the following information.

  4. Save the resource.

You can now invoke the data service query using REST.

Finish creating the data service

Once you have defined the operation, click Finish to complete the data service creation process. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.

Invoking your data service using SOAP

You can try the data service you created by using the TryIt tool that is in your product by default. 

  1. Go to the Deployed Services screen.

  2. Click the Try this service link for the MongoDB data service. The TryIt Tool will open with the MongoDB service.

  3. Select the mongo_insert_op operation and enter values for ID and Name.

  4. Click Send to execute the operation. The data will be inserted to the database.

  5. Now, select the mongo_find_op operation and click Send. The response will be published in the TryIt tool

Invoking your data service using REST

You can send an HTTP GET request to invoke the data service using cURL as shown below.

curl -X GET http://localhost:8280/services/MongoDB.HTTPEndpoint/find

This will return the response in XML.

com.atlassian.confluence.content.render.xhtml.migration.exceptions.UnknownMacroMigrationException: The macro 'next_previous_links' is unknown.