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.

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.

    Data Service NameMongoDB
  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:

    Datasource IDMongoDB
    Datasource TypeMongoDB
  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:

    ServersEnter localhost. This can be 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 NameEnter mydb. 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 RetryControls whether or not to connect. That is, the system retries to connect automatically.
    Connection TimeoutConnection timeout in milliseconds. 0 is default and infinite.
    Max. WaitMax wait time of a blocking thread for a connection.
    Socket TimeoutSocket timeout value. 0 is default and infinite.
    Connections per HostIf the number of connections allowed per host is exceeded, further connections will be blocked.
    Threads Allowed to Block For Connection MultiplierThe 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.

Creating a query to POST data

Follow the steps given below.

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

    Query IDEnter mongo_insert as the query ID.
    DatasourceSelect 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:#}")
  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:

    Query IDEnter mongo_find as the query ID.
    DatasourceSelect 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()
  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:

      Output typeSpecify the format in which the query results should be presented. You can select XML, JSON or RDF. We will use XML for this tutorial.
      Grouped by elementSpecify a grouping for all the output mappings. This will be the XML element that will group the query result. Enter Documents in this field. 
      Row NameSpecify the XML element that should group each individual result. Enter Document in this field.
    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.

    Operation Namemongo_insert_op
    Query IDmongo_insert
  2. Save the operation.
  3. Click Add New Operation and enter the following information.

    Operation Namemongo_find_op
    Query IDmongo_find
  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.

    Resource Pathinsert/{id}
    Resource MethodPOST
    Query IDmongo_insert
  2. Save the resource.
  3. Click Add New Resource and enter the following information.

    Resource Pathfind
    Resource MethodGET
    Query IDmongo_find
  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.