【问题标题】:Check for Existing Document in Mongo在 Mongo 中检查现有文档
【发布时间】:2012-01-02 01:27:42
【问题描述】:

我是 Mongo 的新手,正在使用 Mongoose 作为 ORM。

对于用户注册,我使用电子邮件作为唯一的。我想检查电子邮件是否已在使用,如果没有,则执行一些操作(可能保存,可能下一个注册步骤等)。

对于看似简单的问题,我很抱歉,但我还没有找到答案。

除非它是“upsert”。这意味着插入文档,即使我还没有准备好...

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您是在问如何查找文件?

    db.users.findOne({email => 'foo@example.com'})
    

    如果它返回一些东西,则文档存在。否则,它不会。

    【讨论】:

    • 谢谢——刚刚试了一下,效果很好。 User.findOne({ email: email }, function(err, user) { if (user) { console.log("user exists") } else { console.log("user doesn't exist") }
    • 也可以使用find function + limit(1) blog.serverdensity.com/… User.find({email: email}, {}, {limit: 1}, function...
    猜你喜欢
    • 2016-03-21
    • 2014-10-06
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多