2014-10-31 100 views
0

我是Mongodb的新手。只要阅读文档并播放。我想找到与2014年8月25日之间的单词“MIA”和时间戳值文件,8月28日,2014年我写了这样PHP/MongoDB与复杂查询

$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

+0

我可以看到收集结构吗? – alu 2014-10-31 07:49:37

+0

我已更新问题。 – IFightCode 2014-10-31 07:55:14

回答

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)); 
+0

到目前为止没有运气。 – IFightCode 2014-10-31 08:40:17

+0

嗯。 var_dump(iterator_to_array($ cursor));'输出为空? – alu 2014-10-31 08:48:03

+0

我的不好,错过了一个变量。但是,工作。谢谢 – IFightCode 2014-10-31 08:52:34