Solved. Just change db.test.ensureIndex(['name']) => to db.test.ensureIndex({'name':1});
I was testing MongoDB today. I have test collection with 100 000 documents. Each document has such structure {_id:123123, name: 'foo123123'}
Test code written in PHP
set_time_limit(0);
ini_set('display_errors', 1);
$mongo = new Mongo("mongodb://127.0.0.1:27017");
$db = $mongo->test;
$collection = $db->test;
$start = microtime(true);
for($i=0; $i<10000; $i++){
$obj = $collection->findOne(array('name'=>'foo'.$i));
}
$end = microtime(true);
var_dump($end-$start);
First test without index on name field is 21 seconds Than I add index
db.test.ensureIndex(['name']);
db.test.getIndexes(); [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.test", "name" : "id" }, { "v" : 1, "key" : [ "name" ], "ns" : "test.test", "name" : "0_" } ]
And repeat test. And got 21 seonds again. Why mongo doesn't use index in my case?