天天看點

PHP與MONGODB的結果集操作

MongoCursor MongoCollection::find(array $query,array $fields)

大家應該有發現,在以上的标準查詢指令中,隻有查詢條件和要查詢的字段,那麼“分頁”、“排序”怎麼辦呢?

mongoDB其實是分兩步來做的,第一步用find()來擷取符合條件的結果,第二步才是分頁、排序、分組等等。

獲得結果數量:

http://www.php.net/manual/en/mongocursor.count.php

$cursor = $cull->find();

$count = $cursor->count();

對結果集排序:

http://www.php.net/manual/en/mongocursor.sort.php

<code>$cursor = $cursor-&gt;sort(array("a" =&gt; 1));</code>

分頁擷取結果集:

http://www.php.net/manual/en/mongocursor.skip.php

http://www.php.net/manual/en/mongocursor.limit.php

$cursor = $cursor-&gt;skip(10)-&gt;limiti(20);