0

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 enter image description here

1
  • Can i see the collection structure? Commented Oct 31, 2014 at 7:49

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm. Is var_dump(iterator_to_array($cursor)); output empty?
My bad, miss-typed one variable. However, worked. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.