# Poco::MongoDB

# class Database

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

## Description

Database is a helper class for creating requests. MongoDB works with collection names and uses the part before the first dot as the name of the database.

## Member Summary

Member Functions: authSCRAM, authenticate, count, createIndex, createOpMsgCursor, createOpMsgMessage, name, queryBuildInfo, queryServerHello

## Types Aliases

### FieldIndex

using FieldIndex = std::tuple < std::string, bool >;

name of the field to index, ascending order (true), descending order (false)

### IndexedFields

using IndexedFields = std::vector < FieldIndex >;

Vector of fields to create index on

## Enumerations

### IndexOptions

INDEX_UNIQUE = 1 << 0

INDEX_SPARSE = 1 << 1

INDEX_BACKGROUND = 1 << 2

### WireVersion

Wire version as reported by the command hello. See details in MongoDB github, repository specifications. @see queryServerHello

VER_26 = 1

VER_26_2 = 2

VER_30 = 3

VER_32 = 4

VER_34 = 5

VER_36 = 6

< First wire version that supports OP_MSG

VER_40 = 7

VER_42 = 8

VER_44 = 9

VER_50 = 13

VER_51 = 14

< First wire version that supports *only* OP_MSG

VER_52 = 15

VER_53 = 16

VER_60 = 17

## Constructors

### Database

explicit Database(
const std::string & name
);

Creates a Database for the database with the given name.

## Destructor

### ~Database

virtual ~Database();

Destroys the Database.

## Member Functions

### authenticate

bool authenticate(
Connection & connection,
const std::string & username,
const std::string & password,
const std::string & method = AUTH_SCRAM_SHA1
);

Authenticates against the database using the given connection, username and password, as well as authentication method.

"SCRAM-SHA-1" (default starting in MongoDB 3.0) is the only supported authentication method. "MONGODB-CR" is no longer supported as it requires the legacy wire protocol.

Returns true if authentication was successful, otherwise false.

May throw a Poco::ProtocolException if authentication fails for a reason other than invalid credentials.

### count

[[nodiscard]]
Int64 count(
Connection & connection,
const std::string & collectionName
) const;

Sends a count request for the given collection to MongoDB using OP_MSG protocol.

If the command fails, -1 is returned.

### createIndex

Document::Ptr createIndex(
Connection & connection,
const std::string & collection,
const IndexedFields & indexedFields,
const std::string & indexName,
unsigned long options = 0,
int expirationSeconds = 0,
int version = 0
);

Creates an index. The document returned is the response body.. For more info look at the createIndex information on the MongoDB website. (new wire protocol)

### createOpMsgCursor

[[nodiscard]]
SharedPtr < OpMsgCursor > createOpMsgCursor(
const std::string & collectionName
) const;

Creates OpMsgCursor for the given collection.

### createOpMsgMessage

[[nodiscard]]
SharedPtr < OpMsgMessage > createOpMsgMessage(
const std::string & collectionName
) const;

Creates OpMsgMessage for the given collection.

### createOpMsgMessage

[[nodiscard]]
SharedPtr < OpMsgMessage > createOpMsgMessage() const;

Creates OpMsgMessage for database commands that do not require collection as an argument.

### name

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

Database name

### queryBuildInfo

[[nodiscard]]
Document::Ptr queryBuildInfo(
Connection & connection
) const;

Queries server build info using OP_MSG protocol.

### queryServerHello

[[nodiscard]]
Document::Ptr queryServerHello(
Connection & connection
) const;

Queries hello response from server using OP_MSG protocol.

### authSCRAM

bool authSCRAM(
Connection & connection,
const std::string & username,
const std::string & password
);

## Variables

### AUTH_SCRAM_SHA1

static const std::string AUTH_SCRAM_SHA1;

Default authentication mechanism for MongoDB 3.0 and later.