MongoDB Java nested documents not accessible using dots in key name

*爱你&永不变心* 提交于 2021-02-17 06:26:06

问题


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:


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" } )


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 Documentclass 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:


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.



来源:https://stackoverflow.com/questions/61144095/mongodb-java-nested-documents-not-accessible-using-dots-in-key-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!