那么以下:
new MongoDate(date())
确实有点太细化了,因为您不太可能拥有具有该确切时间戳值的任何东西,而且肯定不是一整天。
您要查找的是今天和第二天之间的“范围”所以首先计算日期:
$date = new DateTime(); # Time right now
$ts = $date->getTimestamp();
$today = $ts - ( $ts % ( 60 * 60 * 24 ) ); # Round to the day
$tomorrow = $today + ( 60 * 60 * 24 ); # Add one day to that
$start = new MongoDate( $today );
$end = new MongoDate( $tomorrow );
然后调用“范围”上的值:
(...)->createQueryBuilder('Document')
->remove()
->field('active')->equals(1)
->where('createdOn')->range( $start, $end )
->getQuery()
->execute();
通常在 MongoDB 查询术语中用$gt
和$lt
运算符表示,但学说 DSL 有一个组合运算符来表示一个范围。