2017-02-16 29 views
0

查找蒙戈3嵌套值我在我的收藏蒙哥的文件,看起来像这样:用java

{ 
    "timestamp": 1460442540, 
    "page": "home", 
    "misc": { 
      "color": "red", 
      "light": "off" 
      } 
} 

我希望能够做的就是回到misc.color的价值时在页面上满足一定的条件。

这是我有:

//find my query match 
    Document value = mongoDBService.getCollection(collectionName) 
      .find(eq("page", "home")).first(); 

    //prints json described above 
    LOG.info(value.toString()); 

    //prints "misc" subdocument 
    LOG.info(value.get("misc").toString()); 

    //null pointer 
    LOG.info(value.get("misc.color").toString()); 

是否有某种方式来处理这里的点号,我很想念?理想情况下,我希望这个查询是动态的,因此它可以处理点符号值和更高级别的值。

我使用的是mongodb-driver 3.4.2。

回答

3

您可以更新您的代码的下方。

Document value = mongoDBService.getCollection(collectionName) 
      .find(eq("page", "home")).first(); 

LOG.info(value.get("misc", Document.class).getString("color")); 
+0

其中是** eq **方法? – subash

+0

对不起哥们。得到它了。它在** Filters **类中。但它让我另一次谷歌搜索。如果你指定,这是更多的帮助 – subash