【问题标题】:How can I check a column for NULL values in node-orm如何在 node-orm 中检查列的 NULL 值
【发布时间】:2016-11-08 10:18:23
【问题描述】:

我在 node.js 中使用 orm 包,并尝试在我的一个模型上使用 find() 查询数据库。但是,我不知道如何使用find() 检查列的 NULL 值。有没有办法做到这一点?

【问题讨论】:

    标签: javascript node.js database orm


    【解决方案1】:

    如果 sent_at 字段在未设置时不存在,则:

    db.emails.find({sent_at: {$exists: false}})
    

    如果它存在并且为空,或者根本不存在:

    db.emails.find({sent_at: null})
    

    参考:Query for nul fields

    注意:由于您没有提及数据库名称,也没有提及假设数据库为 mongodb 时使用的 orm。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      在条件中加入 null 怎么样

      schema.find({ col: null}, function (err, people) {  …  });
      

      schema.count({ col: null }, function (err, count) {
          console.log("We have %d Does in our db", count);
      });
      

      文档还说您可以进行原始查询

      db.driver.execQuery(
        "SELECT * FROM table WHERE col IS NOT NULL",
        function (err, data) { ... }
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-27
        • 2011-08-25
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多