这是我尝试过的:
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("email", email_id);
DBCursor cursor=collection.find(searchQuery);
String usr=null;
String pass=null;
while(cursor.hasNext())
{
usr=(String)cursor.next().get("username");
pass=(String)cursor.next().get("password");
}
我正在获取记录中存在的用户名值。但密码返回null。
答案 0 :(得分:2)
请试试这个:
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("email", email_id);
cursor = collection.find(searchQuery);
DBObject resultElement = null;
resultElement = cursor.next();
if(resultElement.containsField("username")){
String password = (String) resultElement.get("password");
}
希望这有帮助:)
答案 1 :(得分:1)
我认为最好的方法是使用findOne:
DBObject query = new BasicDBObject("email", email_id);
user = collection.findOne(query);
if (user != null)
{
usr= user.get("username").toString();
pass= user.get("password").toString();
}