我有一个 Product
集合存储在 MongoDB 中。它有一个属性date
:
const productSchema = new mongoose.Schema<ProductAttrs>({
date: {
type: Date,
required: true,
}
...
})
我想检索日期
与当前时间相比在过去 6 分钟内的所有产品。在 Mongoose 中检索它的有效方法是什么?
我在我的项目中使用express.js + typescript + mongoose v5.11。
最佳答案
您必须使用 Mongoose 搜索时间大于现在 - 6 分钟
的每条记录。
我无法测试它,但查询应该如下所示:
const sixMinutes = (6*60*1000); // 6 minutes
let sixMinutesAgo = new Date();
sixMinutesAgo.setTime(sixMinutesAgo.getTime() - sixMinutes);
const products = Product.find({
date: {
$gte: sixMinutesAgo
}
})
关于node.js - 检索最近 6 分钟内所有 `date` 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68419082/