简体   繁体   中英

How to use OR condition in mule dataweave for reading data from mongodb

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM