我一直試圖從 MongoDB 中的集合中獲取布爾值,但是當通過 getBoolean 詢問時,我收到 null。在 MongoDB 文檔中有 1 個包含以下信息:name:"Test" booleanValue:trueDocument searchQuery = new Document();searchQuery.put("name", "Test");FindIterable<Document> documents = collection.find(searchQuery);for (Document document: documents) { String name = searchQuery.getString("name"); Boolean booleanValue = searchQuery.getBoolean("booleanValue"); System.out.println(document); System.out.println(name); System.out.println(booleanValue);}它表明它可以在打印所有內容時找到文檔和名稱,甚至可以正確獲取 booleanValue,但是當我 getBoolean 時,我收到 null。Document{{name=Test, booleanValue=true}} 測試 null
1 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
Document searchQuery = new Document();
你創造了Document這里。它沒有任何名為 的密鑰booleanValue。
Boolean booleanValue = searchQuery.getBoolean("booleanValue");
現在您正試圖在此處查詢該對象。當然你不會找到 key 的任何東西booelanValue。您可能將 結果文件誤認為是searchQuery。
for (Document document: documents) {
String name = searchQuery.getString("name");
Boolean booleanValue = document.getBoolean("booleanValue");
System.out.println(document);
System.out.println(name);
System.out.println(booleanValue);
}
您需要改為查詢document。
添加回答
舉報
0/150
提交
取消