Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top