我刚刚意识到 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')}
并忽略了 operator $gte
。在上面的示例中,它尝试将日期与最终引发转换错误的对象进行比较!
更新
实际上是我的错误,忘记在前面加上 $ 符号gte