This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

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.


Start the Create New Data Service wizard

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

Add MongoDB as a datasource

When you get to the Add New Data Source screen, click Add New Datasource to open the corresponding screen and enter the following values:

  • Datasource ID: Enter mongo as the ID for the datasource. 
  • Datasource Type: Select MongoDB as the datasource type from the list. 
  • 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:
    • Servers: A comma separated list of server hosts and ports where the database is running. E.g.: "localhost" - "125.10.5.3, 125.10.5.4" - "192.168.3.1:27017, 192.168.3.2:27017".
    • Database Name:  The name of the database to which you want to connect.
    • Write Concern:  Select NONE from the list. The write concern value to control the write behavior as well as exception raising on error conditions: 

       Descriptions of Write Concern options
      OptionDescription
      FSYNC_SAFEExceptions are raised for network issues, and server errors. The write operation waits for the server to flush the data to disk.
      NONENo exceptions are raised, even for network issues.
      NORMALExceptions are raised for network issues, but not server errors.
      REPLICAS_SAFEExceptions are raised for network issues, and server errors; waits for at least 2 servers for the write operation.
      SAFEExceptions are raised for network issues, and server errors; waits on a server for the write operation.
    • Read Preference: Select PRIMARY from the list. The read preference value, which describes how MongoDB clients route read operations to members of a replica set. It has the following options.

       Descriptions of Read Preference options
      OptionDescription
      PRIMARYDefault mode. All operations read from the current replica set primary.
      SECONDARYAll operations read from the secondary members of the replica set.
    • Auto Connect Retry: Controls whether or not to connect. That is, the system retries to connect automatically.
    • Connection Timeout: Connection timeout in milliseconds. 0 is default and infinite.
    • Max. Wait: Max wait time of a blocking thread for a connection.
    • Socket Timeout: Socket timeout value. 0 is default and infinite.
    • Connections per Host: If the number of connections allowed per host is exceeded, further connections will be blocked.
    • Threads Allowed to Block For Connection Multiplier: The value in this field, multiplied by the connections per host, gives the maximum number of threads that may be waiting for a connection to become available from the pool. All further threads will get an exception. For example, if connections per host are 10 and the 'threads allowed to block for connection multiplier' is 5, up to 50 threads can wait for a connection.

Define a query for the datasource

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.

We will create two queries:

  • mongo_insert: This query will be defined to insert data into the database. We will be adding values for the customer ID and customer Name fields.
  • mongo_find: This query will search for requested data in the database and display them.

To create the mongo_insert query:

  1. Click Add New Query to open the Add New Query screen.
  2. We will first create a query that can add data intothe database. Enter the following values:
    • Query ID: Enter mongo_insert as the query ID.
    • Datasource: Select the datasource for which you are going to write a query. Select the mongo datasource that you created previously.
    • SPARQL: In this field, enter the SPARQL query describing the data that should be retrieved from the RDF datasource. We will use the following query: 

      things.insert("{id:#, name:#}")
  3. 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 as shown below.
    2. Click Add to save the input mapping.
    3. Create the input mapping for the name field.
    4. The two input mappings will be listed as shown below.

      Find out more about defining Input Mappings.

    5. Once you have created both the mappings, click Main Configuration to return to the Edit Query screen.
  4. Click Save, to save the query. 
  5. Click Next to go to the Operations screen.

To create the mongo_find query:

  1. Click Add New Query to open the Add New Query screen.
  2. Enter the following values to define a query that can find data from the database:
    • Query ID: Enter mongo_find as the query ID.
    • Datasource: Select the datasource for which you are going to write a query. Select the mongo datasource that you created previously.
    • SPARQL: In this field, enter the SPARQL query describing the data that should be retrieved from the MongoDB datasource. We will use the following query: 

      things.find
  3. 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. In the Output type field, specify the format in which the query results should be presented. You can select XML, JSON or RDF. We will use XML for this tutorial.
    2. In the Grouped by element field, specify a grouping for all the output mappings. This will be the XML element that will group the query result. Enter Documents in this field. 
    3. In the Row Name field, specify the XML element that should group each individual result.  Enter Document in this field.
    4. Click Add New Output Mapping to start creating the output mapping. Enter the following values:
    5. Click Add to save the mapping.

      Find out more about defining Output Mappings.

    6. Once you have created the output mappings, click Main Configuration to return to the Edit Query screen.

  4. Click Save, to save the query. 
  5. Click Next to go to the Operations screen.

Define operations to invoke the query

Data service operations are written to invoke queries. We will create two operations for this datasource: 

  • mongo_insert_op: This operation will invoke the mongo_insert query.
  • mongo_find_op: This operation will invoke the mongo_find query.

Follow the steps given below.

  1. Click Add New Operation to open the relevant screen.
  2. In the Operation Name field, enter mongo_insert_op.
  3. In the Query ID field, select the mongo_insert query, which you created in the previous step.
  4. Save the operation.
  5. In the same way, create the mongo_find_op operation to invoke the mongo_find query.
  6. You will now have two operations listed.

Finish creating the data service

In this tutorial, we are only defining a SOAP service to expose the data. Therefore, you can click Finish to complete the data service creation process, after defining the operation. You will now be taken to the  Deployed Services screen, which shows all the data services deployed on the server.

If you click Next after defining the operation, you will be taken to the Add Resources screen, which allows you to expose the data as a REST resource. If you want to create a REST resource, see Exposing Data as a REST Resource for information.



Invoking your data service

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