node.js - 获取 24 小时前的 X 条记录 Mongoose
问题描述
我的项目基于 Hapi.js 和 Mongoose。我被困在 24 小时前的 X 价格中。
我以这种方式获得最新添加的记录:
const pricesnow = await Prices.find({
currency_id: {
$in:
currencies[k].id
},
createdAt: {
$gt: new Date().getTime() - 1000 * 60 * 1
}
})
所以我认为我可以使用以下代码从 24 小时前获取 x 记录:
const prices24h = await Prices.find({
currency_id: {
$in:
currencies[k].id
},
createdAt: {
$gt: new Date().getTime() - 86400000,
$lt: new Date().getTime() - 86399999
}
})
但是即使数据库中有数据,24小时前的价格也不会返回。
第二个代码仅在时间跨度在一小时内有效。
之后,我将从 pricenow 中减去 price24h 以计算 24h 百分比。
提前感谢您的回答。
解决方案
您可以使用limit
和sort
功能find
const pricesnow = await Prices.find({
currency_id: {
$in: [], // replace with currency array
},
createdAt: { $gt: new Date(Date.now() - 86400000) },
})
.sort({ createdAt: -1 })
.limit(10);
推荐阅读
- html - 无法点击导航栏中的链接
- cordova - 如何在 Android 设备上运行 ionic cordova?
- ios - 端点不符合亚马逊精确营销活动创建的条件
- python - 将列表转换为 ListNode
- java - java.util.ServiceLoader 在 WebSphere 上不起作用?Eclipse 没有问题
- javascript - 如何按内容获取数组序号?
- javascript - 如何检查对象是否在我的数组中?
- node.js - MongoDB:加载导航时发生错误:拓扑已破坏
- laravel - 我的数据库中的神秘用户不是来自我的注册过程
- python - 应用程序结构应该是什么样子?