我正在使用MongoDB测试我的查询,它运行得很好但是当我想在日期范围内返回数据时我选择它在我没有日期范围的情况下测试时返回相同的数据,所以这意味着它不会考虑我已经过的日期时间的参数。
我在Symfony4,MongoDB项目工作,所以你可以在这里找到我的查询代码在ReactionRepository中:
public function getCommonFans($pages, $users, $from, $to)
{
$builder = $this->dm->createAggregationBuilder(FbReaction::class);
$builder
->match()
->field('fbPage')->in($pages);
->field('fbUser')->in( $users)
->field('created_time')->gte($from)
->field('created_time')->lte($to);
$result = $builder->execute()->toArray();
}
这是控制器:
public function getCommunFansByCompetitor (Request $request){
$reactionRepository = $this->get('doctrine_mongodb')->getManager()
->getRepository(FbReaction::class);
$from=$to=null;
try {
if($request->query->get('from'))
$from = new \DateTime($request->query->get('from'));
if($request->query->get('from'))
$to = new \DateTime($request->query->get('to'));
} catch (\Exception $e) {
}
$listOfCompetitorsPages = $reactionRepository->getCompetitors();
$listOfUsersOfCurrentPage = $reactionRepository->getFansOfCurrentPage();
$result = $reactionRepository->getCommonFans( $listOfCompetitorsPages , $listOfUsersOfCurrentPage , $from, $to);
$response= $this->get('jms_serializer')->serialize($result, 'json');
return new Response($response);
}