当我尝试从mongoDB文档中放入这个type1
数组时,我可以通过执行以下代码将其作为数组:
BasicDBList listEditionsAssets = (BasicDBList) nextAssets.get("areas");
以下是type1
数组:
{
"ClientId" : "212",
"UserId" :"212",
"areas" : [
{
"Area 1" : "Chicago"
"Area 2" : "LA",
"Area 3" : "Boston"
]
}
但是,以下是我在mongoDB文档中的数据:
以下是'type2'数组:
{
"ClientId" : "212",
"UserId" :"212",
"areas" : ["Chicago","LA","Boston" ]
}
当我尝试使用BasicDBList检索java中的“areas”数组时,它变为“null”。因为java将它作为String。当我把它作为
BasicDBObject result = (BasicDBObject) it.next();
String areas = (String) result.get("areas");
我可以读作字符串。
那么如何将type2文档中的“区域”作为java中的数组读取而不是字符串?
答案 0 :(得分:1)
读取数组和读取文档的唯一显着区别在于,由于数组的元素没有名称,因此没有要读取的字段名称,只有一系列值:
reader.readStartArray();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
switch (reader.getCurrentBsonType()) {
case INT32:
int intValue = reader.readInt32();
break;
case INT64:
long longValue = reader.readInt64();
break;
// ... handle each supported field type
}
}
reader.readEndArray();