简体   繁体   English

如何使用给定的时间戳在 mongodb 中查找每天的最后一条记录?

[英]How to find last record of each day in mongodb using given timestamps?

I'm using MongoDB to store data, every 15 mins once one new record will be inserted into DB.So I want to download the report using the given timestamps.我正在使用 MongoDB 存储数据,每 15 分钟一次将一条新记录插入 DB。所以我想使用给定的时间戳下载报告。 If I select the Friday, January 1, 2021 12:00:00 AM( 1609459200000 ) timestamp to Sunday, January 3, 2021 11:59:59 PM( 1609718399000 ) timestamp I should get only 3 records as a response.如果我 select 2021 年 1 月 1 日星期五上午 12:00:00( 1609459200000 )时间戳到 2021 年 1 月 3 日星期日下午 11:59:59( 1609718399000 )时间戳,我应该只得到 3 条记录作为响应。

db.vehicle.aggregate( [ { "$match": { "time": {"$gte":1609459200000,"$lte":1609718399000 }} }, { "$sort": { "time": 1 } }, { $group: { _id: "$speed", time: { $last: "$time" } } } ] ); db.vehicle.aggregate( [ { "$match": { "time": {"$gte":1609459200000,"$lte":1609718399000 }} }, { "$sort": { "time": 1 } } , { $group: { _id: "$speed", time: { $last: "$time" } } } ]);

I tried the above query but it's giving all the records

Thank you.谢谢你。

Example DB Record:示例数据库记录:

// Day 1
{
   "_id":"100",
   "vehical":"train_1",
   "speed":"100kmh",
   "time":1609459200000,
   "starting_point":12
},
{
   "_id":"101",
   "vehical":"train_1",
   "speed":"120kmh",
   "time":1609460100000,
   "starting_point":13
},
{
   "_id":"102",
   "vehical":"train_1",
   "speed":"125kmh",
   "time":1609461000000,
   "starting_point":14
},
// Day 2
{
   "_id":"201",
   "vehical":"train_1",
   "speed":"100kmh",
   "time":1609545600000,
   "starting_point":15
},
{
   "_id":"202",
   "vehical":"train_1",
   "speed":"130kmh",
   "time":1609546500000,
   "starting_point":16
},
// Day 3
{
   "_id":"301",
   "vehical":"train_1",
   "speed":"100kmh",
   "time":1609632000000,
   "starting_point":20
},
{
   "_id":"302",
   "vehical":"train_1",
   "speed":"140kmh",
   "time":1609632900000,
   "starting_point":21
}

Response:回复:

// Day 1
{
   "_id":"102",
   "vehical":"train_1",
   "speed":"125kmh",
   "time":1609461000000,
   "starting_point":14
},
// Day 2
{
   "_id":"202",
   "vehical":"train_1",
   "speed":"130kmh",
   "time":1609546500000,
   "starting_point":16
},
// Day 3
{
   "_id":"302",
   "vehical":"train_1",
   "speed":"140kmh",
   "time":1609632900000,
   "starting_point":21
}

Try this one:试试这个:

db.vehicle.aggregate([
   { $match: { time: { $gte: 1609459200000, $lte: 1609718399000 } } },
   { $set: { timestamp: { $toDate: "$time" } } },
   { $sort: { time: 1 } },
   {
      $group: {
         _id: {
            $dateFromParts: {
               year: { $year: "$timestamp" },
               month: { $month: "$timestamp" },
               day: { $dayOfMonth: "$timestamp" }
            }
         },
         last: { $last: "$$ROOT" }
      }
   },
   { $replaceRoot: { newRoot: "$last" } }
]);

Someone else posted a similar question 7 months ago. 7个月前有人发布了类似的问题。 This might answer your question: How to get last document of each day in MongoDB collection?这可能会回答您的问题: 如何在 MongoDB 集合中获取每天的最后一个文档?

If that doesn't work for you and you know that the last record of the day will always be generated at the same time, you should be able to build a query with $hour and $minute.如果这对您不起作用,并且您知道当天的最后一条记录将始终同时生成,那么您应该能够使用 $hour 和 $minute 构建查询。

https://docs.mongodb.com/manual/reference/operator/aggregation/hour/ https://docs.mongodb.com/manual/reference/operator/aggregation/hour/
https://docs.mongodb.com/manual/reference/operator/aggregation/minute/ https://docs.mongodb.com/manual/reference/operator/aggregation/minute/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM