Search code examples
mongodbdatetimetime-seriesisodate

Date day/minute in mongodb queries


I have time series data stored in a mongodb database, where one of the fields is an ISODate object. I'm trying to retrieve all items for which the ISODate object has a zero value for minutes and seconds. That is, all the objects that have a timestamp at a round hour.

Is there any way to do that, or do I need to create separate fields for hour, min, second, and query for them directly by doing, e.g., find({"minute":0, "second":0})?

Thanks!


Solution

  • You could do this as @Devesh says or if it fits better you could use the aggregation framework:

    db.col.aggregate([
       {$project: {_id:1, date: {mins: {$minute: '$dateField'}, secs: {$second: '$dateField'}}}},
       {$match: {mins: 0, secs: 0}}
    ]);
    

    Like so.