我的情况是我用一个字符串查询MongoDB,该字符串对象层次结构中的深度超过一级.此查询必须是字符串.所以例如我在Groovy中查询类似的东西:
def queryField = 'a.b.c' //this is variable and can be different every time def result = mongodb.collection.findOne([queryField:5])
问题不会出现在结果中我想找到嵌套字段的值.有了GPath,我可以深入一级并获得这样做的价值
def aObj = result."a" //or result["a"]
但是我希望通过做这样的事情来深入研究:
def queryField = "a.b.c" //this can change every time and is not always 'a.b.c' def cObj = result[queryField] //since field is variable,can't just assume result.a.b.c
这在Groovy中不起作用.有一个错误记录here,但我想知道是否有更好的工作来使用这个场景比我通过拆分点然后构建对象遍历解析字符串更清洁.注意,“a.b.c”在运行时是可变的和未知的(例如它可能是“a.b.d”).