On Friday, October 12, 2012 5:16:35 PM UTC-4, Bryan Absher wrote:
I am want to objects that have { b : 2 } and date closest to ISODate("2012-10-12T20:44:50.000Z"). I only want one result back which is _id = 4.
Is the best way just to filter the data after it is returned from open ended date range query? For example timestamp <= ISODate("2012-10-12T20:44:50.000Z").
Is it feasible for you to accomplish this with two queries? You could query with the {b: 2} criteria for both, and then each use $gte and $lte the target date, respectively, sort by the appropriate order (ascending for $gte, descending for $lte) and then limit by 1. In your application, you'd then pick the result closest to your target date.
With one query, I could imagine having a fixed threshold with an $or clause, but that's not guaranteed to match a result (depending on the threshold) and an $or essentially branches into two query paths anyway.