2014-10-31 109 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