【问题标题】:Nodejs Bookshelf. How to use BETWEEN filterNodejs 书架。如何使用 BETWEEN 过滤器
【发布时间】:2016-10-10 18:40:14
【问题描述】:

我想通过元素的时间戳过滤数据库表。

表格有行: ID、URL、时间表。

如何通过某个范围内的时间戳过滤数据? 像往常一样在 SQL ... BETWEEN firstTime 和 lastTime

我尝试了类似的东西:

  if (first_date && last_date) {
        Pages.forge()
            .where("Date", '>=' , first_date)
            .where("Date", '<=' , last_date)
            .fetchAll()
            .then(function (pages) {
                res.send(persons.toJSON());
            }).catch(function (error) {
                console.log(error);
                res.send('An error occured');
            });

【问题讨论】:

标签: node.js bookshelf.js


【解决方案1】:

如果你在调用链中添加.query(),你就可以在里面使用knex语法了。

以你为例:

      Pages.forge()
          .query(function(knex) {
              knex.whereBetween('Date', [first_date, last_date]);
          })
          .fetchAll()
          .then(function (pages) {
              // do something with pages
          }).catch(function (error) {
              // do something with error
          });

【讨论】:

    【解决方案2】:
    model.query({where: {"Date", '>=' , first_date}, orWhere: {"Date", '<=' , last_date}})
          .fetch()
          .then(function(model) {
            // ...
          });
    

    您必须使用 where 和 or where 例如:between 表示两个条件中的任何一个,但在这里您将两个条件都设置为 true 。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2017-12-29
      • 2014-06-08
      • 1970-01-01
      • 2013-03-10
      • 2018-08-17
      • 1970-01-01
      相关资源
      最近更新 更多