【问题标题】:mongoose-paginate module is not supporting query operatorsmongoose-paginate 模块不支持查询运算符
【发布时间】:2019-06-18 17:11:47
【问题描述】:

我刚刚意识到 mongoose-paginate 不支持比较查询运算符https://docs.mongodb.com/manual/reference/operator/query-comparison/

例子:

(created_on: Date)

Post.paginate({
    created_on: {
        gte: new Date('2019-01-01T00:00:00.000Z')
    }
}, {
    page: 2,
    limit: 10,
    sort: {
        created_on: 'desc'
    }
}).then((data) => {
    // do something with docs
});

这会抛出一个错误:

CastError: Cast to date failed for value "{ gte: 2019-01-01T00:00:00.000Z }" at path "created_on" for model "Post"

似乎它将属性created_on 与整个对象{$gte: new Date('2019-01-01T00:00:00.000Z')} 进行比较,并忽略了该运算符$gte。 在上面的示例中,它尝试将日期与最终引发转换错误的对象进行比较!

更新

其实是我搞错了,忘记在gte前面加$符号

【问题讨论】:

    标签: mongoose mongoose-plugins


    【解决方案1】:

    我的错,我在gte之前忘记了$是我的错

    Post.paginate({
        created_on: {
            $gte: new Date('2019-01-01T00:00:00.000Z')
        }
    }, {
        page: 2,
        limit: 10,
        sort: {
            created_on: 'desc'
        }
    }).then((data) => {
        // do something with docs
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2011-07-06
      相关资源
      最近更新 更多