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 :
(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?
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.
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?:
#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.
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.