我有以下json結構。我正在嘗試retreive在java中運行以下mongo查詢,其中hData._id不爲空。MongoDB Java - 嵌套json中獲取ID
MongoDb Query: db.Collection.find({},{"hData._id":1, "hData.createdBy":1})
{
"_id" : ObjectId("55567e594e3256a23565ce58"),
"hData" : {
"isDeleted" : false,
"canDelete" : false,
"canUpdate" : false,
"createdBy" : 「xyz」,
"createdDate" : "2015-05-15T15:05:30",
"_id" : "7"
},
"changeDate" : "2015-02-19T16:02:12",
}
我已用Java編寫的,以獲取hData._id的代碼是
MongoCursor<Document> cur = col.find(new BasicDBObject("hData._id", new BasicDBObject("$ne",null)))).iterator();
try{
while(cur.hasNext()){
System.out.println(cur.next().getObjectId("hData._id"));
i++;
}
}finally {
cur.close();
}
然而,hData._id返回爲null
。你能幫我解決這個問題嗎?
你有沒有檢查cur.next()是什麼?我想你不能調用getObjectId(「hData._id」)。 – vinay
您使用的是哪個版本的mongo驅動程序? – ProgrammerBoy