'mongoDB search query taking long when I use regular expression and case-insensitive

I am using MongoDB\Driver\Manager my query is shown below.

$globalSearchValue = 'Test';
$mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");    
$bulk = new MongoDB\Driver\BulkWrite;
$filter = [ "archival" => 0,   
'$or' => [ ["test1" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test1" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test2" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test3" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test4" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test5" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test6" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test7" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test8" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test9" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test10" => array('$regex' => "$globalSearchValue",'$options'=>'i')],   
        ["test11" => array('$regex' => "$globalSearchValue",'$options'=>'i')],
        ["test12" => array('$regex' => "$globalSearchValue",'$options'=>'i')]
    ];                                                                          
$options = [ 'sort' => ['modified_date' => -1],'limit' => 10];
$qry = new MongoDB\Driver\Query($filter,$options);
$rows = $mongo->executeQuery("testDB.testcollection", $qry);


It is taking more the 50 seconds some times and I tried with index also but no use because when we use regular expression and case-incentive.

Could any one help please if it is one record also taking more time.

My document count is more then 3,00,000 in my collection (testcolletion).

this query I am using for global search,please help on it.

Thanks sandeep



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source