英文:
MongoDB Java nested documents not accessible using dots in key name
问题
使用Java中的MongoDB API时,我试图检索类似以下结构的文档中的two
的值:
data-id: "1234"
one:
two: "three"
我运行了以下代码:
MongoCollection<Document> documents = ...;
Document document = documents.find(Filters.eq("data-id", "1234")).first(); // 不为null
document.get("one"); // 不为null
document.get("one.two"); // 这是null
((Document) document.get("one")).get("two"); // 不为null
在阅读了一些文档和其他Stack Overflow的问题后,我了解到在键名中使用点号(例如one.two
作为键名)应该是有效的,但对我来说却不起作用。
英文:
When using the MongoDB API in Java, I am trying to retrieve the value of two
in a document that looks like this:
data-id: "1234"
one:
two: "three"
And I am running this:
MongoCollection<Document> documents = ...;
Document document = documents.find(Filters.eq("data-id", "1234")).first(); // Not null
document.get("one"); // Not null
document.get("one.two"); // This is null
((Document) document.get("one")).get("two"); // Not null
After spending some time reading documentation and other Stack Overflow questions, I learned that using dots in the key name (like one.two
for the key) should work, but it isn't for me.
答案1
得分: 1
> 在阅读了一些文档和其他 Stack Overflow 的问题后,我了解到在键名中使用点号(例如像 one.two 这样的键)应该是有效的,但实际对我并不起作用。
使用点号表示法在 find
方法的查询过滤器中使用时效果很好。例如,
Document document = collection.find(Filters.eq("one.two", "three")).first();
System.out.println(document); // 打印返回的文档
或其对应的 mongo
shell 代码:
db.collection.find({ "one.two": "three" })
<br>
Document 类的 get()
方法以一个 Object
(即字符串键)作为参数,并返回一个 Object
。
考虑以下代码:
Document doc = coll.find(eq("data-id", "1234")).first();
System.out.println(doc);
输出 Document{{_id=1.0, data-id=1234, one=Document{{two=three}}}}
显示有三个键:_id
、data-id
和 one
。请注意,没有名为 one.two
的键。键 two
位于具有键 one
的文档的子文档中。
所以,在你的代码中:
document.get("one.two"); // 这是 null(Document)
document.get("one")).get("two"); // 不为 null
第一条语句返回 null
,下一条语句返回 three
(字符串值)。这两者都是正确的结果,这是 Document
类 API 的行为。
你应该使用方法 getEmbedded
来访问嵌套字段 one.two
。所以,用以下代码替换 document.get("one.two")
:
document.getEmbedded(Arrays.asList("one", "two"), String.class)
结果是 "three",如预期。
英文:
> After spending some time reading documentation and other Stack
> Overflow questions, I learned that using dots in the key name (like
> one.two for the key) should work, but it isn't for me.
The dot-notation works fine when used within a find
method's query filter. For example,
Document document = collection.find(Filters.eq("one.two", "three")).first();
System.out.println(document); // prints the returned document
or its mongo
shell equivalent:
db.collection.find( { "one.two": "three" } )
<br>
The Document class's get()
method takes an Object
(a String key) as a parameter and returns an Object
.
Consider the code:
Document doc = coll.find(eq("data-id", "1234")).first();
System.out.println(doc);
The output Document{{_id=1.0, data-id=1234, one=Document{{two=three}}}}
shows there are three keys: _id
, data-id
and one
. Note there is no key named as one.two
. The key two
is within the sub-dcument of the document with the key one
.
So, from your code:
document.get("one.two"); // This is null ((Document)
document.get("one")).get("two"); // Not null
The first statement returns null
, and the next one returns three
(the String value). Both are correct results and that is the behavior of the Document
class API.
You should use the method getEmbedded
to access the embedded field one.two
. So, replace document.get("one.two")
with
document.getEmbedded(Arrays.asList("one", "two"), String.class)
The result is "three", as expected.
答案2
得分: 0
MongoDB允许在字段名称中使用点。
document.get("one.two");
实际上会查找类似于
data-id: "1234"
"one.two": "three"
其中"one.two"是一个简单的字段,而不是嵌套文档。
英文:
MongoDB allows dots in the field names.
document.get("one.two");
would actually be looking for a field like
data-id: "1234"
"one.two": "three"
where "one.two" is a simple field, not an embedded document.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论