文字

建立索引

MongoDB 支持索引,并且很容易给集合添加索引。你需要指定字段名和排序方向: 升序(1)或降序(-1)。例如:

<?php
$connection 
= new  MongoClient ();
$collection  $connection -> database -> collectionName ;

$coll -> ensureIndex ( array(  "i"  =>  ) );   // create index on "i"
$coll -> ensureIndex ( array(  "i"  => - 1 "j"  =>  ) );   // index on "i" descending, "j" ascending
?>

当你的数据量变得越来越大,创建索引对读取性能有巨大的提升。如果你不了解索引,可以参考 MongoCollection::ensureIndex() 文档和 MongoDB 的 » MongoDB indexing documentation。

上一篇: 下一篇: