Upserting map using mule mongodb connector

708 Views Asked by At

I am using mule's MongoDB connector to update a document in items collection. The element that I am trying to update in this document is a map as shown below --

 "ratings": {"stars":4.5 , "votes":232},

However, the connector updates the subitem as a String --

 "ratings": "{\"stars\":4.5 , \"votes\":232}",

Here is the flow doing the update --

    <flow name="update-subitem-in-db">
    <logger message="Updating in DB for item #[itemId] -- #[payload] )" level="INFO" doc:name="Logger" />
    <mongo:update-objects-by-function-using-map
        collection="items" config-ref="Mongo_DB" doc:name="Update Object"
        upsert="true" function="$set" multi="false">
        <mongo:query-attributes>
            <mongo:query-attribute key="_id">#[itemId]</mongo:query-attribute>
        </mongo:query-attributes>
        <mongo:element-attributes>
            <mongo:element-attribute key="ratings">#[payload]</mongo:element-attribute>
        </mongo:element-attributes>
    </mongo:update-objects-by-function-using-map>
</flow>

Any suggestions re: how to resolve this issue?

1

There are 1 best solutions below

0
On
    <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>

Then you can call

    #[message.payload.ratings.stars]