Php 查询不';T考虑在参数中传递的日期范围 我用MangGDB测试我的查询,它工作得很好,但是当我想在一个日期范围内返回数据时,我选择在没有日期范围的情况下测试它时返回相同的数据,所以这意味着不考虑我已经通过的日期时间的参数。
我在MongoDB项目的Symfony4工作,因此您可以在这里找到我在ReactionRepository中查询的代码:Php 查询不';T考虑在参数中传递的日期范围 我用MangGDB测试我的查询,它工作得很好,但是当我想在一个日期范围内返回数据时,我选择在没有日期范围的情况下测试它时返回相同的数据,所以这意味着不考虑我已经通过的日期时间的参数。,php,mongodb,doctrine,symfony4,Php,Mongodb,Doctrine,Symfony4,我在MongoDB项目的Symfony4工作,因此您可以在这里找到我在ReactionRepository中查询的代码: public function getCommonFans($pages, $users, $from, $to) { $builder = $this->dm->createAggregationBuilder(FbReaction::class); $builder ->match() ->field('fbPage')-
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);
}
显示您希望匹配的文档和传入的值。很多时候,日期都是字符串,不适合
$gte
和$lte
范围。嘿,尼尔,我在帖子中添加了一个链接,其中有我正在使用的文档图片。当我检查$from的值时,我得到了这个:object(DateTime)#5718(3){[“date”]=>string(26)“2017-01-01 12:10:18.000000”[“timezone_type”]=>int(3)[“timezone”]=>string(3)“UTC”}请不要包含外部链接或图片。以JSON格式显示从mongo
shell查看时显示的数据。Compass不是以可重用状态显示数据的好工具。在您的问题中还包括您发送给控制器的“输入”。不过,乍一看,看起来您的数据实际上存储为BSON Date,不过您可能是在用“字符串”而不是日期进行查询。您需要获取并执行转换。