I have the follwing code
I try to find out if the client is a vlid client in terms of the authentication of the user. In the example i used two different user which give me the same result. Both result in "Connected to DB!", but the first user is not valid. In the old mongo db c++ driver it was possible to check authentication with auth() on a ScopedDBConnection, but this is not possible anymore. How can i test if the client is valid with the provided URI?
I tried to call the authentication command via the run_command() function as follows
bsoncxx::builder::stream::document auth;
auth << "authenticate"
<< bsoncxx::builder::stream::open_document
<< "user" << "foo"
<< "pwd" << "bar"
//<< "digestPassword" << false
//<< "mechanism" << "MONGODB-CR"
<< bsoncxx::builder::stream::close_document;
std::cout << bsoncxx::to_json(auth.view()) << std::endl;
try
{
auto result = db.run_command(auth.view());
std::cout << bsoncxx::to_json(result) << std::endl;
}
catch (const mongocxx::exception & e)
{
std::cerr << "mongocxx::exception - " << e.what() << std::endl;
}
But this always gives me the following error "field missing/wrong type in received authenticate command: generic server error". The documentation for the authentication command is a bit unclear in terms of how the BSON should look like.
https://docs.mongodb.com/manual/reference/method/db.auth/#db.authCan you tell me how to reliable check if a connection to the database with the supplied information was successful or not and how to call the correctly call the authentication command.
Best regards
Henning