0

I'm accessing CosmosDB3.2 via MongoDB connector v5.3.1 in a Mule 4 api.

I've following collection in database:

{ "id": "range", 
  "from": 100, 
  "to": 1, 
  "used": 0 
}

I will fetch data from this collection based upon id=range. However, same time I also want to update, and fetch the updated data - to support multiple simultaneous hits with correct data retrieval.

Search Query:

 %dw 2.0
 output application/json
 ---
 {
     "id" : "range"
 }

Update Query:

%dw 2.0
 output application/json
 ---
 { "\$inc": {    
     "from": -1,
     "used": 1    
 }}

Hence, connector code becomes:

<mongo:find-one-and-update-document doc:name="Find one and update document" doc:id="f7fb0fe6-ae0c-4d82-8766-92d6e7081c0e" config-ref="MongoDB_Config" collectionName="test" returnNewDocument="true">
    <mongo:find-query ><![CDATA[#[vars.findQuery]]]></mongo:find-query>
    <mongo:content-to-update ><![CDATA[#[vars.updateQuery]]]></mongo:content-to-update>
</mongo:find-one-and-update-document>

Expected payload:

 {
     "id": "range",
     "from": 99,
     "to": 1,
     "used": 1
 }

However, I figured that "Find one and update document" connector is not able to accept update function "inc". It's giving following exception:

The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.

Has anyone faced similar issue and have any solution, please help. I think sequence support is not there in mongodb/cosmosdb.

Thanks, Swati Jain

Swati Jain
  • 11
  • 4
  • Hi Swati ,You want to update `from` and `used` values in database, or you would want to display in output payload in dataweave ? – Ven Jul 04 '19 at 08:47
  • Hi @Ven, I want to update both ```from``` and ```used``` in database as per both the Search and Update Queries mentioned, and return the Updated result as payload. – Swati Jain Jul 05 '19 at 20:19

1 Answers1

0

I didn't get solution to the problem mentioned with mule connector itself, however, I was able to have it another way.

I used execute-general-command connector with command findAndModify to achieve same results:

db.runCommand(
   {
     findAndModify: "test",
     query: { "id" : "range" },
     update: { $inc : { "from" : -1, "used": 1 } },
     new: true
   }
 )
Swati Jain
  • 11
  • 4