[英]Convert MongoDB BasicDBList to Java short[] Array
I have inserted a 1D short[] array to MongoDB. 我已经将一维short []数组插入到MongoDB中。 It was easy.Now I am trying to retreive the same array.
这很容易。现在,我正在尝试恢复相同的阵列。 It returns a BasicDBList.I want to do some manipulation on the elements of the list.
它返回一个BasicDBList。我想对列表的元素进行一些操作。 Hence, I want to convert it to Java short[] back.
因此,我想将其转换回Java short []。 How can I do this ?
我怎样才能做到这一点 ?
Following is the code: 以下是代码:
/* Insert the 1DArray*/ / *插入1DArray * /
DB dB = (new MongoClient("localhost",27017)).getDB("Test1DArray");
DBCollection dbcollection = dB.getCollection("Test1DArray");
BasicDBObject aisDocument = new BasicDBObject();
aisDocument.append("TDArray",out1D);
dbcollection.insert(aisDocument);
/* Fetch the 1DArray*/ / *获取1DArray * /
ObjectId MLMatrixObjectsID = (ObjectId)aisDocument.get( "_id" );
System.out.println(MLMatrixObjectsID);
BasicDBObject fields = new BasicDBObject();
fields.put("_id", MLMatrixObjectsID);
DBCollection dbcollectionfetch = dB.getCollection("Test1DArray");
DBCursor cursor = dbcollectionfetch.find(fields);
BasicDBList ODarr=null;
while (cursor.hasNext()) {
ODarr = (BasicDBList)(cursor.next().get("TDArray"));
for(int cell=0; cell < ODarr.size(); cell++){
System.out.println(ODarr.get(cell));
}
}
Any help is appreciated. 任何帮助表示赞赏。 Thank you.
谢谢。
Object[] str = `ODarr.toArray();
short[] x = new short[str.length];
for (int i = 0; i < str.length; i++) {
x[i]=((Integer) str[i]).shortValue();}
Does this help ? 这有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.