MongoCollection
PHP Manual

MongoCollection::commandCursor

(No version information available, might only be in Git)

MongoCollection::commandCursorExecute a database command and retrieve results through a cursor

Description

public MongoCommandCursor MongoCollection::commandCursor ( array $command )

With this method you can execute commands and retrieve the results through a cursor, instead of getting just one document back as you would with MongoDB::command(). 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.

Parameters

command

The command query to send.

It is possible to configure how many initial documents the server should return with the first result set. This configuration is made as part of the command query. The default initial batch size is 101. You can change it by adding the cursor key in the following way to the command:

<?php
$collection
->commandCursor( [
    
"aggregate" => "collectionName",
    
"pipeline" => [
        ...
    ],
    
"cursor" => [ "batchSize" => ],
] );

If you do not set the initial batchSize option, then the PHP driver will implicitly add this for you with a value of 101

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

Return Values

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.

Examples

Example #1 MongoCollection::cursorCommand() "aggregate" 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->commandCursor( [
    
"aggregate" => "people",
    
"pipeline" => [
        [ 
'$group' => [ '_id' => '$name''points' => [ '$sum' => '$points' ] ] ],
        [ 
'$sort' => [ 'points' => -] ],
    ]
] );

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

?>

The above example will output something similar to:


Molly: 130
Joe: 26
Sally: 22

Example #2 MongoCollection::cursorCommand() "aggregate" 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->commandCursor( [
    
"aggregate" => "people",
    
"pipeline" => [
        [ 
'$group' => [ '_id' => '$name''points' => [ '$sum' => '$points' ] ] ],
        [ 
'$sort' => [ 'points' => -] ],
    ],
    
"cursor" => [ "batchSize" => ],
] );

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

?>

The above example will output something similar to:


Molly: 130
Joe: 26
Sally: 22

See Also


MongoCollection
PHP Manual