MongoDB Java嵌套文档无法使用点号在键名中访问

huangapple go评论94阅读模式
英文:

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: &quot;1234&quot;
one:
    two: &quot;three&quot;

And I am running this:

MongoCollection&lt;Document&gt; documents = ...;
Document document = documents.find(Filters.eq(&quot;data-id&quot;, &quot;1234&quot;)).first(); // Not null
document.get(&quot;one&quot;); // Not null
document.get(&quot;one.two&quot;); // This is null
((Document) document.get(&quot;one&quot;)).get(&quot;two&quot;); // 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}}}} 显示有三个键:_iddata-idone。请注意,没有名为 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(&quot;one.two&quot;, &quot;three&quot;)).first();
System.out.println(document);    // prints the returned document

or its mongo shell equivalent:

db.collection.find( { &quot;one.two&quot;: &quot;three&quot; } )

<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(&quot;data-id&quot;, &quot;1234&quot;)).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(&quot;one.two&quot;);    // This is null ((Document)
document.get(&quot;one&quot;)).get(&quot;two&quot;); // 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 Documentclass API.

You should use the method getEmbedded to access the embedded field one.two. So, replace document.get(&quot;one.two&quot;) with

document.getEmbedded(Arrays.asList(&quot;one&quot;, &quot;two&quot;), 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(&quot;one.two&quot;);

would actually be looking for a field like

data-id: &quot;1234&quot;
&quot;one.two&quot;: &quot;three&quot;

where "one.two" is a simple field, not an embedded document.

huangapple
  • 本文由 发表于 2020年4月11日 00:05:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61144095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定