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

时间:2019-04-16 06:56:01

标签: mongodb go do-while

我具有如下的Json结构。

{  
 "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"
     }
   }
 }
}

我想从上面的结构构建Mongo查询,其中“运算符”表示二进制或逻辑表达式,“左”,“右”表示字段和值。但是我无法在无限循环中处理逻辑表达式内的逻辑表达式。任何想法,解决方案的建议都会有很大帮助。

编辑:

    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..
            }
        }

sel很有趣-

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
}

我只是无法在嵌套地图中进行操作,并且在'type'为'LogicalExpression'之前无法了解如何进行迭代,并将查询附加在一起。

0 个答案:

没有答案