$Match range of compound index key?

37 views
Skip to first unread message

Jonas

unread,
Oct 12, 2012, 11:47:22 AM10/12/12
to mongod...@googlegroups.com
Hello.

I have documents with the following format:

time : {
"day": 5,
"month": 10,
"year": 2012,
"hour": 22,
}

If I have a compound index on {year:1,month:1,day:1,hour:1}

Can I find objects in a date range?

In other words

I want 

  2011<=year<=2012

where year is 2011 (but not 2012), I also want
  4<=month

where year is 2012 (but not 2011), I also want
  month<=7

They can at least get it to sort like this using $sort. I want to return objects where the index key is between a certain range using the aggregation framework!

Thank you in advance!
/J

Jeremy Mikola

unread,
Oct 12, 2012, 5:11:40 PM10/12/12
to mongod...@googlegroups.com
If you using the aggregation framework, it may be preferable to work with an actual BSON Date and then use the date operators to exact and project parts of the date as needed.

If you're dealing with individual fields for the data components, I can't think of a simple query to handle that (without some deep $and/$or nesting). It would be much simpler to work with a Date field, which can easily be indexed, and then query between two ranges on that one field. That would remove the need for a multi-key index and certainly prove more efficient (storage and performance-wise).

Jonas

unread,
Oct 15, 2012, 4:36:08 AM10/15/12
to mongod...@googlegroups.com
Thanks Jeremy for your reply.

The reason that I do not use BSON date is that it is not local sensitive. See my previous topic on the issue:


Since sorting in this order is possible you'd think that you could do some greater than/less than on compound indexes. If anyone else has some input please share.

Otherwise I guess the way to go is to add another field that is a timestamp that compensated for the current timezone as an index.
Reply all
Reply to author
Forward
0 new messages