使用java在Mongo3中查找嵌套值
我的mongo收藏中有一个文档如下所示:使用java在Mongo3中查找嵌套值,java,mongodb,mongodb-query,Java,Mongodb,Mongodb Query,我的mongo收藏中有一个文档如下所示: { "timestamp": 1460442540, "page": "home", "misc": { "color": "red", "light": "off" } } 我希望能够在页面上满足特定条件时返回misc.color的值 这就是我所拥有的: //find my query match Document value = mongoDBSer
{
"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驱动程序3.4.2。您可以尝试以下方法:
BasicDBObject query = (BasicDBObject)(value.get("misc")).getString("color");
您可以尝试以下方法:
BasicDBObject query = (BasicDBObject)(value.get("misc")).getString("color");
您可以将代码更新到以下位置
Document value = mongoDBService.getCollection(collectionName)
.find(eq("page", "home")).first();
LOG.info(value.get("misc", Document.class).getString("color"));
您可以将代码更新到以下位置
Document value = mongoDBService.getCollection(collectionName)
.find(eq("page", "home")).first();
LOG.info(value.get("misc", Document.class).getString("color"));
eq法在哪里?对不起,伙计。知道了。它在过滤器类中。但我又花了一次谷歌搜索。如果您指定,它更有用。eq方法在哪里?对不起,伙计。知道了。它在过滤器类中。但我又花了一次谷歌搜索。如果您指定了,它将提供更多帮助