Reputation: 520
I'm pretty new in Mongodb. Just read documentation and played. I wanted to find documents with the word "MIA" and timestamp value between 25th Aug, 2014 to 28th Aug, 2014. I wrote this way
$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);
I was getting empty result. Can you please advice to fix this?
Thanks
EDIT:
Collection Structure
Upvotes: 0
Views: 47
Reputation: 456
Try this.
$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));
Upvotes: 1