[英]get last 2 days/minutes from mongodb array
我有像這樣的mongodb數據:
"_id" : "sensor-2",
"data" : [
{
"sensor" : {
"intensitas" : 3,
"arus" : 0.1
},
"tanggal" : ISODate("2018-08-05T21:00:19.552+07:00")
},
{
"sensor" : {
"intensitas" : 8,
"arus" : 0.1
},
"tanggal" : ISODate("2018-08-05T21:04:40.594+07:00")
},
{
"sensor" : {
"intensitas" : 6,
"arus" : 0.1
},
"tanggal" : ISODate("2018-08-05T21:05:43.647+07:00")
},
{
"sensor" : {
"intensitas" : 13,
"arus" : 0.1
},
"tanggal" : ISODate("2018-08-04T21:05:45.150+07:00")
},
{
"sensor" : {
"intensitas" : 15,
"arus" : 0.05
},
"tanggal" : ISODate("2018-08-04T21:05:46.651+07:00")
},
{
"sensor" : {
"intensitas" : 13,
"arus" : 0.05
},
"tanggal" : ISODate("2018-08-04T21:05:51.152+07:00")
},
{
"sensor" : {
"intensitas" : 8,
"arus" : 0.05
},
"tanggal" : ISODate("2018-08-04T21:05:52.654+07:00")
},
{
"sensor" : {
"intensitas" : 11,
"arus" : 0.05
},
"tanggal" : ISODate("2018-08-04T21:05:58.756+07:00")
},
{
"sensor" : {
"intensitas" : 3,
"arus" : 0.05
},
"tanggal" : ISODate("2018-08-07T21:05:58.757+07:00")
}
]
我想從數組中獲取ID的最后2天或分鍾,而“ tanggal”是英語中的Date。
我已經嘗試過如何從mongoDb過濾最近10天的記錄? 但不是我想要的,因為獲取數據后,我想對過去2天/分鍾的數據進行平均。
要獲得最后2天的時間,您可以執行以下操作:
db.getCollection('items').aggregate([
{
$unwind: '$data'
},
{
$match: {
'data.tanggal': {
$gt: (new Date(new ISODate()-2*1000*60*60*24))
}
}
},
{
$sort: {
'data.tanggal': -1
}
}
])
最后2分鍾,您只需從日期計算中刪除24
,即可得到:
$gt: (new Date(new ISODate()-2*1000*60*60))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.