这是我的 MongoDB 文档的简化模式:
{
"_id": 0,
"config": [{
"property1": "a",
"property2": "b",
"property3": "c",
"property4": "d"
},
{
"property1": "a",
"property2": "bb",
"property3": "cc",
"property4": "d",
"ispropert5": true
},
{
"property1": "a",
"property2": "b",
"property3": "c",
"property4": "ddd",
"ispropert5": false,
"ispropert6": false
}
],
"entity": "123asdf",
"url": "",
"createdDate": 1
}
作为输出,我需要获取嵌套文档的唯一键列表:{property1, property2, property3, property4, ispropert5, ispropert6}
我正在我的课堂上尝试这个,但当然无法将 ArrayList 转换为 Document:
Document dbo = col.find().first();
Set<String> keys = dbo.keySet();
Iterator iterator = keys.iterator();
while(iterator.hasNext()) {
String key = iterator.next().toString();
if(dbo.get(key) instanceof ArrayList){
Document dboNested = (Document) dbo.get(key); //java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.bson.Document
Set<String> keysNested = dboNested.keySet();
System.out.println("KeyNested: " + keysNested);
}
}
小唯快跑啊
江户川乱折腾
饮歌长啸
相关分类