0

Normal regular-Expression query for MongoDB is faster in console mode. For example customer name start with "Ale": this following query is very faster.

db.labels.find({ "customer.name": { $regex: /^Ale/i } },{"label.name":1})

but once i try this same kind of query in PHP its take huge time to load:

    $page =int($_request["p"])
    $limit = 20;
    $skip  = ($page - 1) * $limit;
    $next  = ($page + 1);
    $prev  = ($page - 1);
    $name = "Ale";
    $regexObj = new MongoRegex("/^".$name."/i"); 
    $where = array("customer.name" => $regexObj); 
    $sort  = array('customer.id' => -1);
$result = $collection->find($where)->skip($skip)->limit($limit)->sort($sort);

When i remove $where condition from the query it runs very faster. But ofcorse i need the $where.

Any suggestion or any solution will be appreciated!

5
  • What skip are you using i the query? Is there a index to satisfy the sort as well as the find? You have shown us two separate queries and told us you expect them to run the same without telling us about any of your setup
    – Sammaye
    Commented Jun 30, 2015 at 17:55
  • @Sammaye, The skip is for the pagination. yes "customer.id" is already indexed which i used for sorting. Both of the queries are for the same purpose. first one run very fast in the command mode and 2nd one as u see i applied in my website which works very slow. i showed first query because it works faster though the sorting() and skip() are not there. just to let you know my customer collection size is more than 22GB.
    – mrana
    Commented Jun 30, 2015 at 18:13
  • so any clue why the following quey is so slow: $result = $collection->find($where)->skip($skip)->limit($limit)->sort($sort);
    – mrana
    Commented Jun 30, 2015 at 18:14
  • i have other php queries running very fast. why particularly this query running very slow? $result = $collection->find($where)->skip($skip)->limit($limit)->sort($sort); Does anyone has answer of my question?
    – mrana
    Commented Jul 1, 2015 at 0:17
  • same kind of query working in command mode very faster: db.labels.find({ "label.name": { $regex: /^the/i } }).skip(1).limit(30).sort( { "label.id": -1 });
    – mrana
    Commented Jul 1, 2015 at 0:23

0

Browse other questions tagged or ask your own question.