Mongo
PHP Manual

Mongo::getHosts

(PECL mongo >=1.1.0)

Mongo::getHostsUpdates status for all hosts associated with this

说明

public array Mongo::getHosts ( void )

This method can only be used with a connection to a replica set. It returns the status of all of the hosts in the set and updates the connection information (the updating is invisible to the user).

This is called automatically by the driver every five seconds.

See the query section of this manual for information on distributing reads to slaves.

参数

此函数没有参数。

返回值

Returns an array of information about the hosts in the set. Includes each host's hostname, id in the set, health (1 is healthy), the uptime, and how up-to-date the host is compared to the primary. For example, on a three-member replica set running locally, it might look something like:

Array
(
    [ubuntu:27017] => Array
        (
            [_id] => 0
            [name] => ubuntu:27017
            [health] => 1
            [state] => 1
            [stateStr] => PRIMARY
            [optime] => MongoTimestamp Object
                (
                    [sec] => 1
                    [inc] => 1291155754
                )

            [optimeDate] => MongoDate Object
                (
                    [sec] => 1291155754
                    [usec] => 0
                )

            [self] => 1
        )

    [ubuntu:27019] => Array
        (
            [_id] => 2
            [name] => ubuntu:27019
            [health] => 1
            [state] => 2
            [stateStr] => SECONDARY
            [uptime] => 91928
            [optime] => MongoTimestamp Object
                (
                    [sec] => 1
                    [inc] => 1291155754
                )

            [optimeDate] => MongoDate Object
                (
                    [sec] => 1291155754
                    [usec] => 0
                )

            [lastHeartbeat] => MongoDate Object
                (
                    [sec] => 1291158846
                    [usec] => 0
                )

        )

    [ubuntu:27018] => Array
        (
            [_id] => 1
            [name] => ubuntu:27018
            [health] => 1
            [state] => 2
            [stateStr] => SECONDARY
            [uptime] => 91944
            [optime] => MongoTimestamp Object
                (
                    [sec] => 1
                    [inc] => 1291155754
                )

            [optimeDate] => MongoDate Object
                (
                    [sec] => 1291155754
                    [usec] => 0
                )

            [lastHeartbeat] => MongoDate Object
                (
                    [sec] => 1291158846
                    [usec] => 0
                )

        )

)

This returns NULL if the connection is not connected to a replica set or has not been initialized, yet.


Mongo
PHP Manual