在 mongodb 中查找没有光标的条目?

发布于 2024-12-24 23:58:46 字数 569 浏览 2 评论 0原文

我使用 mongodb 和 java。有一个功能登录,该功能正在检查数据库中的邮件和适合的密码。如果没有则返回 false,如果有则返回 true:

boolean login(String email, String password) {

    /**/

    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    query.put("password", password);

    DBCollection collection = c.getDatabase().getCollection("...");
    DBCursor cursor = collection.find(query);

    if (cursor.size() > 0)
        return true;
    return false;
}

我感觉这个函数看起来很丑^^ 有没有更好的方法在 java 中编写这个代码?避免古鲁?嗯,它的工作原理,但可能有更好的方法,如 cursor.size() 谢谢

Im using mongodb and java. There is a function login, this function is checking the db for a mail and fit password. If there is no one then return false, if yes then true:

boolean login(String email, String password) {

    /**/

    BasicDBObject query = new BasicDBObject();
    query.put("email", email);
    query.put("password", password);

    DBCollection collection = c.getDatabase().getCollection("...");
    DBCursor cursor = collection.find(query);

    if (cursor.size() > 0)
        return true;
    return false;
}

I have the feeling that this function looks ugly ^^ Is there a better way to code this in java? Avoid a curos? Well its working but there could be a better way as cursor.size() Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-12-31 23:58:46

我不知道它在 Java 中是如何工作的,但是,findOne() 方法应该返回单个文档,如果没有这样的文档,则返回 null。

I don't know how it works in Java, however, the findOne() method should return a single document or null if there is no such document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文