Java MongoDB为子文档获取价值

编程入门 行业动态 更新时间:2024-10-17 15:32:42
本文介绍了Java MongoDB为子文档获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从子文档中获取键的值,而且由于该键被深深地嵌入了两个级别,因此我似乎无法弄清楚如何使用BasicDBObject.get()函数.这是文档的结构

I am trying to get the value of a key from a sub-document and I can't seem to figure out how to use the BasicDBObject.get() function since the key is embedded two levels deep. Here is the structure of the document

File { name: file_1 report: { name: report_1, group: RnD } }

基本上,一个文件包含多个报告,我需要检索给定文件中所有报告的名称.我可以执行BasicDBObject.get("name")并且可以获取值"file_1",但是我该怎么做这样的BasicDBObject.get("report.name")呢?我试过了,但是没用.

Basically a file has multiple reports and I need to retrieve the names of all reports in a given file. I am able to do BasicDBObject.get("name") and I can get the value "file_1", but how do I do something like this BasicDBObject.get("report.name")? I tried that but it did not work.

推荐答案

您应该首先获取报告"对象,然后访问其内容.您可以在下面看到示例代码.

You should first get the "report" object and then access its contents.You can see the sample code in the below.

DBCursor cur = coll.find(); for (DBObject doc : cur) { String fileName = (String) doc.get("name"); System.out.println(fileName); DBObject report = (BasicDBObject) doc.get("report"); String reportName = (String) report.get("name"); System.out.println(reportName); }

更多推荐

Java MongoDB为子文档获取价值

本文发布于:2023-11-22 13:37:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:价值   文档   Java   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!