Poco::MongoDB

class ServerDescription

Library: MongoDB
Package: MongoDB
Header: Poco/MongoDB/ServerDescription.h

Description

Represents the state of a single MongoDB server in a replica set.

This class stores metadata about a MongoDB server obtained from the 'hello' command response (requires MongoDB 5.1 or later), including: - Server type (primary, secondary, arbiter, etc.) - Replica set membership information - Round-trip time for server selection - Server tags for tag-based read preferences

THREAD SAFETY: This class is NOT thread-safe. External synchronization is required if instances are accessed from multiple threads.

Member Summary

Member Functions: address, error, hasError, isPrimary, isSecondary, isWritable, lastUpdateTime, markError, operator !=, operator =, operator ==, reset, roundTripTime, setAddress, setName, tags, type, typeToString, updateFromHelloResponse

Enumerations

ServerType

MongoDB server type enumeration

Unknown

Server type not yet determined

Standalone

Standalone MongoDB instance (not in a replica set)

RsPrimary

Replica set primary (writable)

RsSecondary

Replica set secondary (read-only)

RsArbiter

Replica set arbiter (no data, votes only)

RsOther

Other replica set member type

RsGhost

Ghost member (removed or not yet initialized)

Mongos

MongoDB sharding router

Constructors

ServerDescription

ServerDescription();

Creates an Unknown server description.

ServerDescription

explicit ServerDescription(
    const Net::SocketAddress & address
);

Creates an Unknown server description for the given address.

ServerDescription

ServerDescription(
    const ServerDescription & other
);

Copy constructor.

ServerDescription

ServerDescription(
    ServerDescription && other
) noexcept;

Move constructor.

Destructor

~ServerDescription

~ServerDescription();

Destroys the ServerDescription.

Member Functions

address inline

[[nodiscard]]
const Net::SocketAddress & address() const;

Returns the server address.

error inline

[[nodiscard]]
const std::string & error() const;

Returns the last error message, or empty string if no error.

hasError inline

[[nodiscard]]
bool hasError() const;

Returns true if the last update attempt resulted in an error.

isPrimary inline

[[nodiscard]]
bool isPrimary() const;

Returns true if this is a primary server (RsPrimary) or a standalone server. Standalone servers are treated as primaries for read preference purposes.

isSecondary inline

[[nodiscard]]
bool isSecondary() const;

Returns true if this is a secondary server.

isWritable inline

[[nodiscard]]
bool isWritable() const;

Returns true if this server can accept write operations. Only primary servers are writable.

lastUpdateTime inline

[[nodiscard]]
Timestamp lastUpdateTime() const;

Returns the timestamp of the last successful update.

markError

void markError(
    const std::string & errorMessage
);

Marks this server as having an error. This sets the type to Unknown and stores the error message.

operator !=

bool operator != (
    const ServerDescription & other
) const;

Inequality comparison operator.

operator =

ServerDescription & operator = (
    const ServerDescription & other
);

Assignment operator.

operator =

ServerDescription & operator = (
    ServerDescription && other
) noexcept;

Move assignment operator.

operator ==

bool operator == (
    const ServerDescription & other
) const;

Equality comparison operator. Compares type, address, setName, and error state.

reset

void reset();

Resets the server description to Unknown state.

roundTripTime inline

[[nodiscard]]
Poco::Int64 roundTripTime() const;

Returns the round-trip time in microseconds. This is used for "nearest" read preference selection.

setAddress inline

void setAddress(
    const Net::SocketAddress & address
);

Sets the server address.

setName inline

[[nodiscard]]
const std::string & setName() const;

Returns the replica set name, or empty string if not in a replica set.

tags inline

[[nodiscard]]
const Document & tags() const;

Returns the server tags for tag-based read preferences. Returns an empty document if no tags are configured.

type inline

[[nodiscard]]
ServerType type() const;

Returns the server type.

typeToString static

[[nodiscard]]
static std::string typeToString(
    ServerType type
);

Converts a server type enum to a human-readable string. Returns "PRIMARY", "SECONDARY", "ARBITER", "STANDALONE", "MONGOS", "OTHER", "GHOST", or "UNKNOWN".

updateFromHelloResponse

[[nodiscard]]
std::vector < Net::SocketAddress > updateFromHelloResponse(
    const Document & helloResponse,
    Poco::Int64 rttMicros
);

Updates the server description from a 'hello' command response. The rttMicros parameter should contain the round-trip time of the hello command in microseconds. Returns a list of all replica set members (hosts, passives, arbiters) discovered in the hello response.