在我的項目中,我創建了一個名爲ClickScoreTuple的類,它擴展了BasicDBObject。 (否則它會有Serielizable問題)。如何將從MongoDB檢索的DBObject轉換爲自定義類?
然後,我用下面的代碼插入ClickScoreTuple
public void insertToMongodb(String q, ClickScoreTuple cs){
BasicDBObject doc = new BasicDBObject();
doc.put("query", q);
doc.put("clicks", cs);
coll.insert(doc);
}
,並使用下面的代碼從MongoDB中檢索數據。
public ClickScoreTuple retrieveFromMongodb(String q){
BasicDBObject query = new BasicDBObject();
query.put("query", q);
DBCursor cursor = coll.find(query);
**ClickScoreTuple result = (ClickScoreTuple) cursor.next();**
cursor.close();
return result;
}
不過,我得到這樣的問題:
Exception in thread "main" java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to ClickScoreTuple
at MongoDBManager.retrieveFromMongodb(MongoDBManager.java:50)
at UserLogHistoryProcessor.processLogHistory(UserLogHistoryProcessor.java:30)
at UserLogHistoryProcessor.main(UserLogHistoryProcessor.java:108)
有誰知道鋤頭去解決呢?