PHPTS:一键免费搭建 Nginx + PHP + MySQL 运行环境

MongoCommandCursor::getReadPreference

(PECL mongo >=1.6.0)

MongoCommandCursor::getReadPreferenceGet the read preference for this command

说明

public MongoCommandCursor::getReadPreference ( void ) : array

参数

此函数没有参数。

返回值

This function returns an array describing the read preference. The array contains the values type for the string read preference mode (corresponding to the MongoClient constants), and tagsets containing a list of all tag set criteria. If no tag sets were specified, tagsets will not be present in the array.

范例

Example #1 MongoCommandCursor::getReadPreference() return value example

<?php

$m 
= new MongoClient('mongodb://rs1.example.com:27017', array('replicaSet' => 'myReplSetName'));
$collection $m->selectCollection('test''people');

// If a MongoCommandCursor is constructed directly, it will inherit the read
// preference of the MongoClient instance passed to its constructor; however,
// MongoCollection::aggregateCursor() will have the MongoCommandCursor inherit
// the collection's read preference.
$collection->setReadPreference(MongoClient::RP_SECONDARY);

$cursor $collection->aggregateCursor( [
    [ 
'$group' => [ '_id' => '$name''points' => [ '$sum' => '$points' ] ] ],
    [ 
'$sort' => [ 'points' => -] ],
] );

var_dump($cursor->getReadPreference());

?>

以上例程会输出:

array(1) {
  ["type"]=>
  string(9) "secondary"
}

参见