# Poco::MongoDB

# class Connection

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

## Description

Represents a connection to a MongoDB server using the MongoDB wire protocol.

See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ for more information on the wire protocol.

## Member Summary

Member Functions: address, connect, disconnect, sendRequest

## Types

### Ptr

typedef Poco::SharedPtr < Connection > Ptr;

## Constructors

### Connection

Connection();

Creates an unconnected Connection.

Use this when you want to connect later on.

### Connection

Connection(
const std::string & hostAndPort
);

Creates a Connection connected to the given MongoDB instance at host:port.

The host and port must be separated with a colon.

### Connection

Connection(
const Poco::Net::SocketAddress & addrs
);

Creates a Connection connected to the given MongoDB instance at the given address.

### Connection

Connection(
const Poco::Net::StreamSocket & socket
);

Creates a Connection connected to the given MongoDB instance using the given socket, which must already be connected.

### Connection

Connection(
const std::string & host,
int port
);

Creates a Connection connected to the given MongoDB instance at host and port.

## Destructor

### ~Connection

virtual ~Connection();

Destroys the Connection.

## Member Functions

### address

Poco::Net::SocketAddress address() const;

Returns the address of the MongoDB server.

### connect

void connect(
const std::string & hostAndPort
);

Connects to the given MongoDB server.

The host and port must be separated with a colon.

### connect

void connect(
const std::string & host,
int port
);

Connects to the given MongoDB server.

### connect

void connect(
const Poco::Net::SocketAddress & addrs
);

Connects to the given MongoDB server.

### connect

void connect(
const Poco::Net::StreamSocket & socket
);

Connects using an already connected socket.

### disconnect

void disconnect();

Disconnects from the MongoDB server.

### sendRequest

void sendRequest(
RequestMessage & request
);

Sends a request to the MongoDB server.

Used for one-way requests without a response.

### sendRequest

void sendRequest(
RequestMessage & request,
ResponseMessage & response
);

Sends a request to the MongoDB server and receives the response.

Use this when a response is expected: only a "query" or "getmore" request will return a response.

### connect

void connect();