我一直在尝试从mongoDB中的集合中获取布尔值,但是当通过getBoolean询问时,我会收到null
在mongoDB文档中,具有以下信息的为1: 名称:“测试” booleanValue:true
Document 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,但是当我得到布尔值时,我会收到null。
文档{{name = Test,booleanValue = true}} 测试 空
答案 0 :(得分:1)
Document searchQuery = new Document();
您已经在此处创建了Document
。它没有任何称为booleanValue
的密钥。
Boolean booleanValue = searchQuery.getBoolean("booleanValue");
现在您要在此处查询该对象。当然,您将找不到密钥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
。