2014-09-04 444 views
0

我有一些在mongodb中的字段,例如name作爲字符串,id作爲整數,address作爲arraylist等等。我需要名稱爲「什麼數據類型」。我需要獲取名稱數據類型。使用Java從MongoDB獲取字段數據類型

這是我的編碼配置了NetBeans和MongoDB

MongoClient mongoClient = new MongoClient(mongopath); 
DB db = mongoClient.getDB(dbname); 
DBCollection coll = db.getCollection(dbname); 
DBCursor cursor = coll.find(); 

有人能解釋一下嗎?

回答

0

你可以試試下面:

BasicDBObject doc = cursor.hasNext() ? cursor.next() : new BasicDBObject(); 
Object name = doc.get("name"); 
String typeString = name == null ? "null" : name.getClass().toString(); 
+0

感謝ü嚮導。它做工精細..我得到了下面的輸出類java.lang.String ..我需要的字符串數據類型只。你能解釋一下嗎? – BasK 2014-09-05 04:12:04

+0

@Bass:String typeString = name == null? 「null」:name.getClass()。getSimpleName(); – Wizard 2014-09-05 08:30:58

+0

謝謝你嚮導和我有其他疑問,請檢查此網址http://stackoverflow.com/questions/25665118/get-document-word-file-content-from-azure-storage-using-java – BasK 2014-09-05 08:41:51