Qore mongodb Module 2.3.0
Loading...
Searching...
No Matches
mongodb::MongoClient Class Reference

The MongoClient class provides MongoDB client connection management. More...

#include <QC_MongoClient.dox.h>

Public Member Methods

 constructor (string uri)
 Creates a MongoDB client connection from a URI string.
 
MongoDatabase getDatabase (string name)
 Returns a MongoDatabase object for the given database name.
 
*string getUri ()
 Returns the connection URI string.
 
list< string > listDatabaseNames ()
 Returns a list of database names on the server.
 
bool ping ()
 Pings the MongoDB server to verify connectivity.
 

Detailed Description

The MongoClient class provides MongoDB client connection management.

This class wraps a MongoDB client connection and provides methods for database access and server management operations.

Example:
# Connect to MongoDB
MongoClient client("mongodb://localhost:27017");
# Ping the server
if (client.ping()) {
print("Connected successfully\n");
}
# List databases
list<string> dbs = client.listDatabaseNames();
# Get a database handle
MongoDatabase db = client.getDatabase("mydb");
Since
mongodb 1.0

Member Function Documentation

◆ constructor()

mongodb::MongoClient::constructor ( string  uri)

Creates a MongoDB client connection from a URI string.

Parameters
urithe MongoDB connection URI (e.g., "mongodb://localhost:27017")
Example:
# Simple connection
MongoClient client("mongodb://localhost:27017");
# Connection with authentication
MongoClient client("mongodb://user:password@localhost:27017/mydb?authSource=admin");
# Replica set connection
MongoClient client("mongodb://host1:27017,host2:27017/?replicaSet=rs0");
Exceptions
MONGODB-CLIENT-ERRORfailed to parse URI or create client

◆ getDatabase()

MongoDatabase mongodb::MongoClient::getDatabase ( string  name)

Returns a MongoDatabase object for the given database name.

Parameters
namethe database name
Returns
a MongoDatabase object
Example:
MongoDatabase db = client.getDatabase("mydb");

◆ getUri()

*string mongodb::MongoClient::getUri ( )

Returns the connection URI string.

Returns
the MongoDB connection URI
Code Flags:
CONSTANT
Example:
string uri = client.getUri();

◆ listDatabaseNames()

list< string > mongodb::MongoClient::listDatabaseNames ( )

Returns a list of database names on the server.

Returns
a list of database names
Example:
list<string> dbs = client.listDatabaseNames();
foreach string db in (dbs) {
printf("Database: %s\n", db);
}
Exceptions
MONGODB-CLIENT-ERRORfailed to list databases

◆ ping()

bool mongodb::MongoClient::ping ( )

Pings the MongoDB server to verify connectivity.

Returns
True if the ping succeeded
Example:
if (client.ping()) {
print("Server is reachable\n");
}
Exceptions
MONGODB-CLIENT-ERRORping failed

The documentation for this class was generated from the following file: