mongodb - Mule Mongo Connector to produce INNER JOIN results -
i have 2 collections, 1 of organizations , 1 of brands within organization, , trying produce result set have brands related organization show as nested json inside organization json result.
the code getting results looks this, , return expected results, final json has quotes in each brad. how can include brands' json without including string?
query single org mongo id mongo:
<mongo:find-one-object-using-query-map config-ref="mdb_config" collection="orgs" doc:name="fetch object mongo"> <mongo:query-attributes> <mongo:query-attribute key="_id">#[new org.bson.types.objectid(flowvars.orgid)]</mongo:query-attribute> </mongo:query-attributes> </mongo:find-one-object-using-query-map> <mongo:db-object-to-map doc:name="map mongo object hashmap"/>
query orgs brands list:
<foreach collection="#[payload.brands]" doc:name="for each" rootmessagevariablename="rootmessage"> <mongo:find-one-object-using-query-map config-ref="mdb_config" collection="brands" doc:name="fetch brands org"> <mongo:query-attributes> <mongo:query-attribute key="_id">#[new org.bson.types.objectid(message.payload)]</mongo:query-attribute> </mongo:query-attributes> </mongo:find-one-object-using-query-map > <!-- transform brand mongo json schema compliant json --> <scripting:transformer doc:name="map mongo object json"> <scripting:script engine="groovy"><![cdata[ def builder = new groovy.json.jsonbuilder() def root = builder { brandid payload._id.tostring() //... isactive payload.platformheader.isactive } return builder.toprettystring(); ]]></scripting:script> </scripting:transformer> <expression-component>brandresponses.add(message.payloadas(java.lang.string)) </expression-component> <logger message="the result #[message.payloadas(java.lang.string)]" level="info" doc:name="logger"/> </foreach> <logger level="info" message="#[brandresponses]"/> <!-- transform org mongo json schema compliant json --> <scripting:transformer doc:name="map mongo object json"> <scripting:script engine="groovy"><![cdata[ def brandspayload = flowvars.brandresponses def builder = new groovy.json.jsonbuilder() def root = builder { orgid payload._id.tostring() //... isactive payload.platformheader.isactive brands(brandspayload.collect {it}) } return builder.toprettystring(); ]]></scripting:script> </scripting:transformer>
resulting json
{ "orgid": "5565f305b85c31182a65a6a7", "isactive": true, "brands": [ "{ "brandid": "5565f2ff03758e0c189a753d", "isactive": true }", "{ "brandid": "5565f2ff03758e0c189a7594", "isactive": true }" ] }
the problem comes fact store string representation of brands in brandresponses
, collect them strings in final json.
note producing json, there's no big gain in using groovy. building maps/lists mel , serializing them json objects/arrays json:object-to-json-transformer
way easier (as comparison, grooxy easier xml generation).
in case, fix issue:
- remove first
scripting:transformer
- replace expression component store brands in maps instead of strings, with:
brandresponses.add(["brandid":payload._id.tostring(), "isactive":payload.platformheader.isactive])
i believe should it. if not, may need review collect
closure generate json properly.
Comments
Post a Comment