0

I am using MongoDB through the Casbah driver and I am storing documents which contain a date field, which is in my java world a Java DateTime. I would need to perform similar queries:

  • Remove all records with day any but hour falling into a certain interval
  • Remove all records with day any but hour not a multiple of 3.
  • Find all records with day any and hour 11

Is there a simple way to do it , given that the conversion helpers for Joda DateTime store a single field? Should I probably refactor it to have a Time Field and a Date Field?

Edmondo
  • 19,559
  • 13
  • 62
  • 115

1 Answers1

1

The range operators on date times will only work on a single distinct range from year-month-day-hour-etc downwards, so wouldn't work for your queries where you're effectively asking for many distinct ranges.

MongoDB doesn't have a type representing just a time without date either, so possible alternatives would be:

Slightly hacky: index another Date field with year/month/day 'clipped' to the same value, and the hour/min/sec taken from the existing fields (so all dates collapse to a single day), then a single range can match the hours on that day.

Alternative: index another field with the time represented as a string, and construct regex matches for the various conditions (eg between 2-4am inclusive: /^{02,03,04}/).

For either solution you can index the field and the queries should make efficient use of indexes, I believe. The 2nd solution seems slightly more sensible.

barnybug
  • 643
  • 1
  • 5
  • 8