node.js - 检索所有“日期”在过去 6 分钟内的人
问题描述
我有一个Product
存储在 MongoDB 中的集合。它有一个属性date
:
const productSchema = new mongoose.Schema<ProductAttrs>({
date: {
type: Date,
required: true,
}
...
})
我想检索date
与当前时间相比在过去 6 分钟内的所有产品。在 mongoose 中检索它的有效方法是什么?
我在我的项目中使用 express.js + typescript + mongoose v5.11。
解决方案
您必须使用 Mongoose 搜索每个时间大于 的记录now - 6 minutes
。
我无法测试它,但查询应该是这样的:
const sixMinutes = (6*60*1000); // 6 minutes
let sixMinutesAgo = new Date();
sixMinutesAgo.setTime(sixMinutesAgo.getTime() - sixMinutes);
const products = Product.find({
date: {
$gte: sixMinutesAgo
}
})
推荐阅读
- jenkins - 如何使用管道脚本在 jenkins 中检查特定节点的在线或离线状态?
- python - 通过 Python 安装 ImageMagick 和 Ghostscript
- mysql - XAMPP 不会自动将 MySQL 添加到服务中?
- ios - 使用条件和限制安排未来的 UNUserNotificationCenter (iOS)
- c# - 尝试使用视图模型中的命令通过数据绑定更新图像按钮中的图像源 - 不起作用
- c# - 我收到异常导航失败,因为浏览器已断开连接
- java - org.json.JSONObject.getNumber() 使用 unittest 调用时抛出“NoSuchMethodError”
- javascript - 猜数字
- hash - 更改已签名 exe 证书哈希的字节
- asp.net-core - 在 .net core 3 中查询 .dbf 文件?