I have been trying to connect to a cluster in MongoDB Atlas using the mongodb:Client
. I am not able to find any connection string
that is supported by the Ballerina client. I could not find any sample code that suggests how to do so.
Following is the source code
import ballerinax/mongodb;
configurable string app = ?;
configurable string pwd = ?;
mongodb:Client mongoCli = check new ({connection: {url: string `mongodb+srv://${app}:${pwd}@fina-a-journey.ugfjnsm.mongodb.net/?retryWrites=true&w=majority`}});
public function main() {
mongodb:Error? insert = mongoCli->insert({name: "Jhon", age: 16}, "users");
}
3
Answers
The password in the connection string that I had passed to the Client had some special characters that should be escaped using %. After escaping it worked. It is specified here https://www.mongodb.com/docs/atlas/troubleshoot-connection/#special-characters-in-connection-string-password
Please refer to https://lib.ballerina.io/ballerinax/mongodb/4.0.0/records/ConnectionConfig
You may try this:
You may try this
I have tried this in an service endpoint.