简体   繁体   中英

Lithium & MongoDB: Finding documents by date range

I have numerous documents containing a field called "date" which is simply a unix timestamp. whithin lithium, i want to find all documents in a given date range. i'm currently trying the following:

//$_stats contains two \DateTime objects which are properly initialized

$transactions = Transactions::all(
  array('conditions' => array(
                'tags' => array('$all' => array((string)$tag->_id)),
                'date' => array('$gte' => array((int)$_stats['date_start']->getTimestamp()), '$lte' => array((int)$_stats['date_end']->getTimestamp()))
  ))
);

But this returns zero documents. When I remove the "date" condition, it works fine and I get all documents.

What am I missing?

Thanks, aenogym

There doesn't seem to be any need of giving an array of dates, so perhaps try:

$transactions = Transactions::all(
  array('conditions' => array(
            'tags' => array('$all' => array((string)$tag->_id)),
            'date' => array('$gte' => (int)$_stats['date_start']->getTimestamp(), '$lte' => (int)$_stats['date_end']->getTimestamp())
  ))
);

Keep in mind that MongoDate stores dates as miliseconds while timestamp uses seconds. In other words MongoDate has higher precision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM