谷歌镜像 伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: MongoCollection::aggregateCursor

来源:网络转载 浏览:29914次 时间:2023-11-20
MongoCollection::batchInsert » « MongoCollection::aggregate
  • PHP 手册
  • 函数参考
  • 数据库扩展
  • 针对各数据库系统对应的扩展
  • Mongo
  • 核心类
  • MongoCollection

MongoCollection::aggregateCursor

(PECL mongo >=1.5.0)

MongoCollection::aggregateCursor — Execute an aggregation pipeline command and retrieve results through a cursor

说明

public MongoCollection::aggregateCursor ( array $command [, array $options ] ) : MongoCommandCursor

With this method you can execute Aggregation Framework pipelines and retrieve the results through a cursor, instead of getting just one document back as you would with MongoCollection::aggregate(). This method returns a MongoCommandCursor object. This cursor object implements the Iterator interface just like the MongoCursor objects that are returned by the MongoCollection::find() method.

Note: The resulting MongoCommandCursor will inherit this collection's read preference. MongoCommandCursor::setReadPreference() may be used to change the read preference before iterating on the cursor.

参数

pipeline

The Aggregation Framework pipeline to execute.

options

Options for the aggregation command. Valid options include:

  • "allowDiskUse"

    Allow aggregation stages to write to temporary files

  • "cursor"

    It is possible to configure how many initial documents the server should return with the first result set. The default initial batch size is 101. You can change it by adding the batchSize option:

    <?php
    $collection->aggregateCursor( 
        $pipeline,
        [ "cursor" => [ "batchSize" => 4 ] ]
    );

    This option only configures the size of the first batch. To configure the size of future batches, please use the MongoCommandCursor::batchSize() method on the returned MongoCommandCursor object.

  • "explain"

    Return information on the processing of the pipeline. This option may cause the command to return a result document that is unsuitable for constructing a MongoCommandCursor. If you need to use this option, you should consider using MongoCollection::aggregate().

  • "maxTimeMS"

    Specifies a cumulative time limit in milliseconds for processing the operation on the server (does not include idle time). If the operation is not completed by the server within the timeout period, a MongoExecutionTimeoutException will be thrown.

返回值

Returns a MongoCommandCursor object. Because this implements the Iterator interface you can iterate over each of the results as returned by the command query. The MongoCommandCursor also implements the MongoCursorInterface interface which adds the MongoCommandCursor::batchSize(), MongoCommandCursor::dead(), MongoCommandCursor::info() methods.

范例

Example #1 MongoCollection::aggregateCursor() example

Finding all of the distinct values for a key.

<?php
$m = new MongoClient;
$db = $m->test;
$people = $db->people;
$people->drop();

$people->insert(array("name" => "Joe", "points" => 4));
$people->insert(array("name" => "Molly", "points" => 43));
$people->insert(array("name" => "Sally", "points" => 22));
$people->insert(array("name" => "Joe", "points" => 22));
$people->insert(array("name" => "Molly", "points" => 87));

$ages = $people->aggregateCursor( [
        [ '$group' => [ '_id' => '$name', 'points' => [ '$sum' => '$points' ] ] ],
        [ '$sort' => [ 'points' => -1 ] ],
] );

foreach ($ages as $person) {
    echo "{$person['_id']}: {$person['points']}\n";
}

?>

以上例程的输出类似于:


Molly: 130
Joe: 26
Sally: 22

Example #2 MongoCollection::aggregateCursor() example with different initial batch size

Finding all of the distinct values for a key.

<?php
$m = new MongoClient;
$db = $m->test;
$people = $db->people;
$people->drop();

/* Insert some sample data */
$people->insert(array("name" => "Joe", "points" => 4));
$people->insert(array("name" => "Molly", "points" => 43));
$people->insert(array("name" => "Sally", "points" => 22));
$people->insert(array("name" => "Joe", "points" => 22));
$people->insert(array("name" => "Molly", "points" => 87));

/* Run the command cursor */
$ages = $people->aggregateCursor(
    [
        [ '$group' => [ '_id' => '$name', 'points' => [ '$sum' => '$points' ] ] ],
        [ '$sort' => [ 'points' => -1 ] ],
    ],
    [ "cursor" => [ "batchSize" => 4 ] ]
);

foreach ($ages as $person) {
    echo "{$person['_id']}: {$person['points']}\n";
}

?>

以上例程的输出类似于:


Molly: 130
Joe: 26
Sally: 22

参见

  • MongoDB::command() - 执行一条 Mongo 指令
  • MongoCommandCursor
  • MongoCommandCursor::batchSize() - Limits the number of elements returned in one batch
  • MongoCollection::aggregate() - Perform an aggregation using the aggregation framework
  • The MongoDB » aggregation framework
add a note

User Contributed Notes 2 notes

up down 2 Dmitriy1 year ago aggregateCursor with mongoDB 3.6 can cause error:
"the command cursor did not return a correctly structured response"

to fix it on 64 bit systems, temporarily turn on:
ini_set('mongo.long_as_object', true);
up down 1 lukasz at endai dot com4 years ago It looks like maxTimeMS option in the latest release (1.5.8) is not valid anymore. Set \MongoCursor::$timeout before you call aggregateCursor() add a note

官方地址:https://www.php.net/manual/en/mongocollection.aggregatecursor.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net