我有一个从 MongoDB 检索 bson.Document 的 java 代码。JSON/BSON 文档示例如下:
{
"field1": "text1",
"field2": {
"field2Sub1": "text2"
}
}
在我的java代码中,我像这样操作它来获取field1
和field2Sub1
com.fasterxml.jackson.databind.ObjectMapper objectMapper;
org.bson.Document documentFromMongo = this.getDocumentFromMongo();
org.bson.Document field2Document = documentFromMongo.get("field2", Document.class);
String field1Value = objectMapper.convertValue(documentFromMongo.get("field1"), String.class);
String field2Sub1Value = objectMapper.convertValue(field2Document.get("field2Sub1"), String.class);
有什么方法或一些库或方法可以用来获取 field2Sub1 的值,如下所示:
String field1Value = objectMapper.convertValue(documentFromMongo.get("field2.field2Sub1"), String.class);