device_attributes.insert("{ "deviceId": #, "vin": #, "clusterDetails.provisioned": #, "cl" /> device_attributes.insert("{ "deviceId": #, "vin": #, "clusterDetails.provisioned": #, "cl" /> device_attributes.insert("{ "deviceId": #, "vin": #, "clusterDetails.provisioned": #, "cl"/>

Insert object into MongoDB wso2 Data service

119 Views Asked by At
<query id="mongo_insert" useConfig="fetchmongo">
      <expression>device_attributes.insert("{
    "deviceId": #,
    "vin": #,
        "clusterDetails.provisioned": #,
        "clusterDetails.sbEndpoint": #,
        "clusterDetails.clusterName": #,
        "clusterDetails.clusterId": #,
        "clusterDetails.nbEndpoint": #,
        "clusterDetails.kubeMaster": #,
        "clusterDetails.status": #
}")</expression>
      <param name="deviceId" sqlType="INTEGER"/>
      <param name="vin" sqlType="STRING"/>
      <param name="provisioned" sqlType="STRING"/>
      <param name="sbEndpoint" sqlType="STRING"/>
      <param name="clusterName" sqlType="STRING"/>
      <param name="clusterId" sqlType="STRING"/>
      <param name="nbEndpoint" sqlType="STRING"/>
      <param name="kubeMaster" sqlType="STRING"/>
      <param name="status" sqlType="STRING"/>
   </query>

this is my query to insert the data into MongoDB data service. My payload is having object like this trying to insert json its not working so i converted into xml and trying to insert.but its not taking the values object inside object. how to read those values in query

{
    "deviceId": 11111,
    "vin": "WRTHRKSHS0000011",
    "clusterDetails": {
        "provisioned": "N",
        "sbEndpoint": "192.168.22.22",
        "clusterName": "clustertwo",
        "clusterId": "cluster11",
        "nbEndpoint": "192.168.22.22",
        "kubeMaster": "192.168.22.22",
        "status": "D"
    }
}

And XML object.

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <deviceId>111111</deviceId>
    <vin>WERTRKSHS0000011</vin>
    <clusterDetails>
        <provisioned>N</provisioned>
        <sbEndpoint>192.168.22.22</sbEndpoint>
        <clusterName>clustertwo</clusterName>
        <clusterId>cluster10</clusterId>
        <nbEndpoint>192.168.22.22</nbEndpoint>
        <kubeMaster>192.168.22.22</kubeMaster>
        <status>D</status>
    </clusterDetails>
</root>

this payload i am giving in body error i am gettingenter image description here

1

There are 1 best solutions below

0
sanoJ On

In the current implementation of data services, only the first-level data is read to extract the parameters, In your case since you have provisioned as an inner value, the DSS won't be able to extract it. You can follow one of the following to fix your issue,

Option 1: If possible change the request structure so all the data are in the same first level as follows

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <deviceId>111111</deviceId>
    <vin>WERTRKSHS0000011</vin>
    <provisioned>N</provisioned>
    <sbEndpoint>192.168.22.22</sbEndpoint>
    <clusterName>clustertwo</clusterName>
    <clusterId>cluster10</clusterId>
    <nbEndpoint>192.168.22.22</nbEndpoint>
    <kubeMaster>192.168.22.22</kubeMaster>
    <status>D</status>
</root>

Option 2: Use the dataServiceCall mediator to call the DSS

  1. To use the you need an operation to the Dataservice as follows,
<operation name="mongo_insert_op" returnRequestStatus="true">
    <call-query href="mongo_insert">
        <with-param name="deviceId" query-param="deviceId"/>
        <with-param name="vin" query-param="vin"/>
        <with-param name="provisioned" query-param="provisioned"/>
        <with-param name="sbEndpoint" query-param="sbEndpoint"/>
        <with-param name="clusterName" query-param="clusterName"/>
        <with-param name="clusterId" query-param="clusterId"/>
        <with-param name="nbEndpoint" query-param="nbEndpoint"/>
        <with-param name="kubeMaster" query-param="kubeMaster"/>
        <with-param name="status" query-param="status"/>
    </call-query>
</operation>
  1. Create an API to front the DSS and expose it to the clients. You may need to set the serviceName and the operation name accordingly.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/mongoDSS_API" name="mongoDSS_API" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <dataServiceCall serviceName="ADD_YOUR_DSS_NAME_HERE">
                <operations type="single">
                    <operation name="mongo_insert_op">
                        <param evaluator="json" expression="$.deviceId" name="deviceId"/>
                        <param evaluator="json" expression="$.vin" name="vin"/>
                        <param evaluator="json" expression="$.clusterDetails.provisioned" name="provisioned"/>
                        <param evaluator="json" expression="$.clusterDetails.sbEndpoint" name="sbEndpoint"/>
                        <param evaluator="json" expression="$.clusterDetails.clusterName" name="clusterName"/>
                        <param evaluator="json" expression="$.clusterDetails.clusterId" name="clusterId"/>
                        <param evaluator="json" expression="$.clusterDetails.nbEndpoint" name="nbEndpoint"/>
                        <param evaluator="json" expression="$.clusterDetails.kubeMaster" name="kubeMaster"/>
                        <param evaluator="json" expression="$.clusterDetails.status" name="status"/>
                    </operation>
                </operations>
                <source type="inline"/>
                <target type="body"/>
            </dataServiceCall>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

For more information,