略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoCursor::sort

2024-11-14

MongoCursor::sort

(PECL mongo >=0.9.0)

MongoCursor::sortSorts the results by given fields

说明

public MongoCursor::sort ( array $fields ) : MongoCursor

参数

fields

An array of fields by which to sort. Each element in the array has as key the field name, and as value either 1 for ascending sort, or -1 for descending sort.

Each result is first sorted on the first field in the array, then (if it exists) on the second field in the array, etc. This means that the order of the fields in the fields array is important. See also the examples section.

返回值

Returns the same cursor that this method was called on.

错误/异常

Throws MongoCursorException if this cursor has started iterating.

范例

Example #1 MongoCursor::sort() example

<?php
// Sort on field x, ascending
$cursor->sort(array('x' => 1));

// The order in the associative array is important. For instance, these two
// examples will yield different results:

// Sort on date ascending and age descending
$cursor->sort(array('date' => 1'age' => -1));

// Sort on age descending and date ascending
$cursor->sort(array('age' => -1'date' => 1));
?>
add a note add a note

User Contributed Notes 1 note

up
0
caleb dot yang dot work at gmail dot com
11 months ago
This library is dated, I've found the descending sort does not work with the Date type in MongoDB.

Just a gotcha for anyone on this lib

官方地址:https://www.php.net/manual/en/mongocursor.sort.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3