0
votes

Mule 3.8.5 + Data-weave code query :-

I am trying to fetch records from mongodb which is equal to the code passed in input query param OR if the code is equal to static String "ALL".

the above is not working, can anyone suggest the correct syntax to write in transform message component?

%dw 1.0
%output application/json
---
 {
    "code" : [ upper inboundProperties."http.query.params".code or "ALL"]
 }

Example if i pass code as "IND" it should return me records having code equal to "IND" or "ALL". result = IND, ALL, ALL.

1
updated the answer based on the comments.Ryan Carter

1 Answers

2
votes

You can use default:

%dw 1.0
%output application/json
---
 {
    "code" : [ upper inboundProperties."http.query.params".code default "ALL"]
 }

UPDATE.

Based on the mongo query syntax here: https://docs.mongodb.com/manual/tutorial/query-documents/

db.inventory.find( { status: { $in: [ "A", "D" ] } } )

Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

This should generate the json query in the correct format:

%dw 1.0
%output application/json
%var code = inboundProperties."http.query.params".code
---
{
    "code" : { "\$in": [ upper code, "ALL" ] when code !=null otherwise  ["ALL"] }
}

You might need to convert it to a string first before being used by the mongo query.