duan19911992 2019-04-16 06:56
浏览 125

嵌套地图结构中的Golang Building Mongo查询

I have Json structure like below.

{  
 "type":"LogicalExpression",
 "operator":"&&",
 "left":{  
   "type":"BinaryExpression",
  "operator":">",
  "left":{  
     "name":"stage3_num1",
     "type":"Identifier"
  },
  "right":{  
     "value":50,
     "raw":"50",
     "type":"Literal"
  }
 },
 "right":{  
  "type":"LogicalExpression",
  "operator":"||",
  "left":{  
     "type":"BinaryExpression",
     "operator":">",
     "left":{  
        "name":"stage3_num2",
        "type":"Identifier"
     },
     "right":{  
        "type":"Literal",
        "value":200,
        "raw":"200"
     }
  },
  "right":{  
     "type":"BinaryExpression",
     "operator":"<=",
     "left":{  
        "type":"Identifier",
        "name":"stage3_num3"
     },
     "right":{  
        "value":2000,
        "raw":"2000",
        "type":"Literal"
     }
   }
 }
}

I want to build Mongo query from above structure where 'operator' denotes binary or logical expression, and 'left','right' denotes fields and value. But I am unable to handle logical expression inside logical expression in an infinte loop. Any ideas, suggestion towards solution will be a great help.

Edit:

    query := bson.M{}
    filter := jsonmap
        for key, val := range filter {
            if key == "type" && val == "BinaryExpression" {
                a := sel(filter)
                query = a
            } else if key == "type" && val == "LogicalExpression" {
                mongoparam := GetmongoParam(query["operator"].(string))
                query[mongoparam] = []bson.M{}
                //Further operations..
            }
        }

Where sel is func -

func sel(query map[string]interface{}) bson.M {
    result := make(bson.M, len(query))
    mongoparam := GetmongoParam(query["operator"].(string))
    var left string
    var right interface{}
    md, ok := query["left"].(map[string]interface{})
    if ok {
        if md["type"] == "Identifier" {
            left = md["name"].(string)
        }
    }
    rd, ok := query["right"].(map[string]interface{})
    if ok {
        if rd["type"] == "Identifier" {
            right = rd["name"]
        } else {
            right = rd["value"]
        }
    }
    result[left] = bson.M{mongoparam: right}
    return result
}

I am just unable to proceed in nested maps and not getting how to iterate till 'type' is 'LogicalExpression', and append queries together.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab中使用gurobi时报错
    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂