PHP / MongoDB&复杂查询

时间:2014-10-31 07:44:52

标签: php mongodb mongodb-query

我是Mongodb的新手。只需阅读文档并播放。我想在2014年8月25日到2014年8月28日期间找到带有“MIA”和时间戳值的文件。我这样写了

$var = "MIA";
date_default_timezone_set('UTC');
$from = strtotime('2014-08-25');
$to = strtotime('2014-08-28');;

$var = new MongoRegex("/$var/");
$where1 = array( '$or' => array( array(array('caller' => $var)), array('callee' => $var)));

$where2 = array( '$and' => array( array('timestamp' => array('$gte' => $from )), array('timestamp' => array('$lte'=> $to)) ) );

$where = array( '$and' => $where1, $where2 );

//var_dump($where);

$cursor = $collection->find($where);
var_dump($cursor);

我的结果空洞了。你能建议解决这个问题吗?

由于

编辑: 集合结构 enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个。

$where1 = array(
    '$or' => array(
        // array(array('caller' => $var)) => often nest one
        array('caller' => $var),
        array('callee' => $var)
    )
);

$where2 = array(
    '$and' => array(
        array('timestamp' => array('$gte' => $from)),
        array('timestamp' => array('$lte'=> $to))
    )
);

// $where = array( '$and' => $where1, $where2 ); => `$where2` is not in $and query
$where = array( '$and' => array($where1, $where2));