Tobi
Tobi

Reputation: 1313

MongoDB: Query by Time part of Datetime

I have a lot of data in a mongodb with dates in it. Usually these dates are at any date but at the time between 10:00:00 and 22:00:00.

Because I have figured out some strange behaviour during times out of that above range, I want a query that returns every document that is not between 10:00:00 and 22:00:00 at any date. Is there any way to do this in mongodb?

e.g. query by the time part of the date?

Upvotes: 0

Views: 934

Answers (1)

Nadav
Nadav

Reputation: 624

Querying documents that have time after 12:29 am, my solution was:

(event_start is isodatetime)

I) project hour and minute

{'$project': {'_id': 1,
             'hour': {'$hour': '$event_start'}, 
             'minute': {'$minute': '$event_start'}}},

II) Match after that:`

 {'$match': {'$or': [
                     {'hour': {'$gt': 12}},
                     {'$and': [{'hour': {'$eq': 12}},
                               {'minute': {'$gte': 30}}
                              ]}]}}

Source -> Link

Upvotes: 1

Related Questions