[英]Get X records from 24 hours ago 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);
試試這個鏈接希望對你有幫助
db.getCollection("COLLECTION_NAME").find({"createdAt":{$gt:new Date(Date.now() - 24*60*60 * 1000)}})
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.