【问题标题】:find query with a condition to a sub property desn't find the model查找带有子属性条件的查询 找不到模型
【发布时间】:2013-01-12 02:17:38
【问题描述】:

我想提出一个简单的findOne 请求,我在尝试对“子属性”设置条件时遇到问题。

我试图找到的模型确实存在于我的 bdd 上,但我每次都去 else 案例:

Model.findOne({ email : { value: 'toto@gmail.com' } }, 'username email', function(error, User){
    if (error){
        console.log('Error query.');
    }else if (User){
        console.log('User found from email.'); //Expected to go here
    }else{
        console.log('Any user found.'); //Goes here everytime
    }
});

我想这很正常,因为我的其他不在“子属性”上的查询运行良好,那么该怎么做(从“子属性”中查找模型)?

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    您需要使用dot notation 来查询子文档的某个字段:

    Model.findOne({ 'email.value': 'toto@gmail.com' }, ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多