Connection String vs URL for an endpoint

Please help me understand MongoDB connections and networking better.

I developed a sensor using the Arduino IDE and signed up for a free Cluster on Mongodb and created my own database to develop and test the code. I’ve successfully implemented what I wanted. Using Atlas/Mongodb and its Data API , I created an endpoint in the form of a URL and plugged that into the code.

I use Https POST requests in my Arduino code to send to the database so I have definitions and constructs such as :

#define MyMONGO_DBASE "https://us-east-2.aws.data.mongodb-api.com/app/data-xxxxx/endpoint/ur_sensor/do"

char serverAddressM[] = "us-east-2.aws.data.mongodb-api.com";

const char resourceM[] = "/app/data-xxxxx/endpoint/ur_sensor/do";

HttpClient shttp(sclient, serverAddressM, sport);

sclient.connect(serverAddressM, 443)

sclient.print(String("POST ") + resourceM + " HTTP/1.1\r\n");

sclient.print(String("Host: ") + serverAddressM + "\r\n");

etc……

sclient.println(postData);

(I can share my code but it’s about 1500 lines long so I thought I’d first try just showing the relevant constructs)

I understand what is going with my code finding its way to the endpoint using the URL.

So now I want to give the sensor and code to someone else that have their own MongoDB. I obviously need to change the connection path. They gave me a login and password for access to their database through Compass in the form of : mongodb+srv://:@theirorg.xtcr1.gcp.mongodb.net/?retryWrites=true&w=majority. I believe that is referred to as a URI.

I can log into Compass with this connection string and I created a database (called sensors) and collection (called DO) in Compass, much like mine own database.

But NOW WHAT? I don’t have an Atlas connection to this new database and the Atlas features that I originally used in my own DB to create a URL.

I’m told I only need this connection string. I don’t see a lot of options through Compass. The connection string Compass gives me is the same as the login so its implied that is all I need. I understand how to code with a URL but how do I change my code to use this connection string without Atlas? Is it true this is all I need? I don’t need a URL?

Can someone help me make this leap and explain what I need to do to my original code using just a connection string to send the data to the database?..OR how to get a URL through only Compass?

Good afternoon, welcome to the MongoDB community.

What error are you making? Access to the Atlas platform is private, so it is necessary to grant access to the cluster from your IP to the project, do you understand?

I have no error to discuss because I don’t know how to re-code from using a URL to this connection string, that is the question. I can’t try anything until I understand that. I do understand that eventually the person that owns the new database would need grant permission to the sensor box based on its IP. I would tell them that. However, we haven’t gotten to that point yet because my main question remains , do I need a URL or can the connection string be used? If the connection string itself can be used, how is that coded?. Are you implying the person needs to grant me access to their Atlas? If I had that access I could then create an endpoint point like I did with my prototype BUT I wasn’t given access because they said all I need is the connection string.

Hi, thanks for clarifying. Really the only thing you need is a connection string + releasing your IP to the cluster.

In the case of Atlas it is a URI:
mongodb+srv://server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB

OK, you are confirming only the URI is needed…but that is the part I don’t understand. Are you saying in my Arduino code I would make the following assignments?:

char serverAddressM[] = "mongodb+srv://server.example.com/?";
const char resourceM[] = "connectTimeoutMS=300000&authSource=aDifferentAuthDB";

Then use it like I did the URL?..

HttpClient shttp(sclient, serverAddressM, sport);
sclient.connect(serverAddressM, 443)
sclient.print(String("POST ") + resourceM + " HTTP/1.1\r\n");
sclient.print(String("Host: ") + serverAddressM + "\r\n");
etc....

Like this:

#include <SPI.h>
#include <Ethernet.h>
#include <MongoDB.h>

// MongoDB server configuration
IPAddress serverIP(192, 168, 1, 100);  // Replace with your MongoDB server's IP address
int serverPort = 27017;               // MongoDB default port

// MongoDB client
MongoClient client(serverIP, serverPort);

void setup() {
  Ethernet.begin(mac);  // Replace 'mac' with your Ethernet shield's MAC address
  Serial.begin(9600);
}

void loop() {
  if (client.connect("mydb")) {  // Connect to the "mydb" database
    // Perform MongoDB operations here

    client.stop();  // Close the connection when done
  }
  delay(10000);  // Delay for 10 seconds before attempting another connection
}

MongoDB typically doesn’t use HTTP for connections. Instead, it uses its own binary protocol.

Interesting, I never read that Http isn’t typical. Perhaps I used the wrong examples to build on.
So where does the connection string come in?

It is not common to use a connection string in the same way you would in more complete programming languages, such as Python, Node.js, or C#. Creating a connection string in the conventional way, such as in high-level languages, is not supported directly on Arduino due to limitations of available resources and libraries.

#include <SPI.h>
#include <Ethernet.h>
#include <MongoDB.h>

// Define connection information
char serverAddress[] = "192.168.1.100"; // MongoDB server IP address
int serverPort = 27017; // MongoDB port
char dbName[] = "mydb"; // Database name
char username[] = "your_username"; // Username (if needed)
char password[] = "your_password"; // Password (if needed)

// MongoDB client
MongoClient client(serverAddress, serverPort);

void setup() {
  Ethernet.begin(mac); // Replace 'mac' with your Ethernet shield's MAC address
  Serial.begin(9600);
}

void loop() {
  if (client.connect(dbName, username, password)) {
    // Perform MongoDB operations here

    client.stop(); // Close the connection when done
  }
  delay(10000); // Wait for 10 seconds before attempting another connection
}

I have been reading about mongdb+srv, seed lists and connection pools . So your comment on the connection string not being supported directly on Arduino and its libraries I now interpret to be that Arduino libraries, like the http client libraries for instance, need updating to handle the new syntax and the features the seed lists provide. Using these connection strings with this syntax can bring advantages like more robust connections.
It’s unfortunate Arduino doesn’t directly support this. It’s not that Mongodb can’t be used with Arduino, my functioning application proves it can, it is just it can’t be used in this way. I think that means I really need to use a URL in my current implementation, because I can’t use the connection string in my implementation.
Alternatively, I need to re-design the code to be closer to the coding you show that uses direct IP addresses and ports and move away from http. By the way, your code examples show a call to MongoDB.h and MongoClient constructs, where is that coming from? I tried looking in some examples under the C++ drivers but I haven’t run across anything like that yet.

Please let me know if my interpretations and understandings are correct.

This guy doesn’t exist, I took an adaptation of C and transformed it, but that’s what it was, I hadn’t noticed.

Hello Kurt,

MongoDB does not have a driver for Arduino, so your approach using HTTP is the right one.

MongoDB offers App-Services, and you can use HTTPS calls to connect and operate on your current database. Please follow the link here.

I am not familiar with Arduino, but if it supports HTTPS, this is a good work-around. The Data API endpoints do not use SRV records.

Cheers,

Jorge

1 Like