The MongoClient class provides MongoDB client connection management.
More...
#include <QC_MongoClient.dox.h>
|
| | 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.
|
| |
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:
MongoClient client("mongodb://localhost:27017");
if (client.ping()) {
print("Connected successfully\n");
}
list<string> dbs = client.listDatabaseNames();
MongoDatabase db = client.getDatabase("mydb");
- Since
- mongodb 1.0
◆ constructor()
| mongodb::MongoClient::constructor |
( |
string |
uri | ) |
|
Creates a MongoDB client connection from a URI string.
- Parameters
-
| uri | the MongoDB connection URI (e.g., "mongodb://localhost:27017") |
- Example:
MongoClient client("mongodb://localhost:27017");
MongoClient client("mongodb://user:password@localhost:27017/mydb?authSource=admin");
MongoClient client("mongodb://host1:27017,host2:27017/?replicaSet=rs0");
- Exceptions
-
| MONGODB-CLIENT-ERROR | failed to parse URI or create client |
◆ getDatabase()
Returns a MongoDatabase object for the given database name.
- Parameters
-
- 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-ERROR | failed 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-ERROR | ping failed |
The documentation for this class was generated from the following file: