请问对于MongoDB数据库对于帆软参数下拉多选框的适配,拉下不选如何设置mongoDB查询全部,多选值时可以用$in
1. 参数不选查询全部数据的实现当参数为空时,可通过动态调整查询条件实现全部查询。MongoDB语法示例如下:
{"聚合操作": [{"$match": {{{if len(城市) > 0}}"城市字段": { "$in": ${城市} }{{else}}// 不添加过滤条件{{end}}}}]}
注:该语法需要在「服务器数据集」中通过高级设置实现,使用{}作为无参时的查询条件6
2. 多选使用$in查询当选中有值时,使用MongoDB原生的$in运算符处理多选参数:
{"聚合操作": [{"$match": {"城市字段": { "$in": ${城市} }}}]}
{ "$match": { "wms": { "$cond": { "if": { "$gt": [ { "$size": "$iswms" }, 0 ] }, "then": { "$in": "$iswms" }, "else": { "$exists": true } } } } }
除了 $match,后续的 $group、$project 等管道也需要严格遵循 JSON 格式
{ "$group": { "_id": { "wms": "$wms", "order_code": "$order_code" }, "count": { "$sum": 1 } } }
{ "$project": { "_id": 0, "wms": "$_id.wms", "order_code": "$_id.order_code", "count": 1 } }
注意:确保帆软参数 iswms 是数组类型
检查模板格式:确保花括号 {}、逗号 ,、引号 " 都是 JSON 标准的(帆软中模板必须是合法 JSON)