Connection to MongoDB Atlas with Delphi

Viewed 164

I'm using Delphi Seattle with Firedac and I need to connect to my base which is on MongoDB Atlas.

On Firedac I only have the option to put the server's ip, but Atlas only provides a connection string.

How to connect to MongoDB Atlas with Firedac + Delphi?

PS: I tried to get the part of the connection string that references the server address, "mongodb+srv://address.address.mongodb.net/myFirstDatabase", but delphi cannot resolve this address

1 Answers

I was unable to get to work as well, Instead, I used the REST API instead. eg

RESTClient1.BaseURL := 'https://data.mongodb-api.com';
RESTRequest1.Client := RESTClient1;
RESTRequest1.Resource := '/app/data-cemfo/endpoint/data/v1/action/findOne';
RESTRequest1.Method := rmPOST;
RESTRequest1.AddAuthParameter('api-key', 'FHZ....your API key', 
   pkHTTPHEADER, [poDoNotEncode]);
RESTRequest1.AddParameter('Content-Type', 'application/json', pkHTTPHEADER);
RESTRequest1.AddParameter('Access-Control-Request-Headers', '*', 
   pkHTTPHEADER);

body := TJSONObject.Create;
body.AddPair ('collection', 'mycollection');
body.AddPair ('database', 'mydbs');
body.AddPair ('dataSource', mydsrc');

subObj := TJSONObject.Create;
subObj.AddPair ('numInventory', 10);
body.AddPair ('filter', subObj);
RESTRequest1.AddBody(body);

RESTRequest1.Execute;
jsonStr := RESTResponse1.JSONValue.ToString;
Related