简体   繁体   English

如何在Mule DataWeave中使用OR条件从MongoDB中读取数据

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

Mule 3.8.5 + Data-weave code query :- Mule 3.8.5 +数据编织代码查询:-

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". 我试图从mongodb获取记录,该记录等于在输入查询参数中传递的代码,或者如果代码等于静态字符串“ 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". 例如,如果我将代码传递为“ IND”,则它应返回代码等于“ IND”或“ ALL”的记录。 result = IND, ALL, ALL. 结果= IND,ALL,ALL。

You can use default : 您可以使用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/ 基于此处的mongo查询语法: 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. 尽管可以使用$ or运算符表示此查询,但是在同一字段上执行相等性检查时,请使用$ in运算符而不是$ or运算符。

This should generate the json query in the correct format: 这应该以正确的格式生成json查询:

%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. 您可能需要先将其转换为字符串,然后再由mongo查询使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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