I am doing a training exercise that is supposed to connect to a Azure Cosmos DB for MongoDB database using Node.JS however it doesn't seem that the connection is being established and while I'm new to Node.JS/Cosmos DB/Mongo, it doesn't appear that any error/exception is being thrown.
Here is a snippet of the code that opens the connection (I added line breaks to connstring for readibility):
var connstring = "mongodb://jobinfoaccount:xxxxxx
@xxxxx.mongo.cosmos.azure.com:10255/ssl=true
&replicaSet=globaldb&retrywrites=false
&maxIdleTimeMS=120000
&appName=@xxxxx@"
MongoClient.connect(connstring, function(err, client) {
if (err) {console.log(err)}
console.log("connected to Cosmos DB Mongo API");
db = client.db("xxxxx");
//listen to port 3001
app.listen(3001, () => {
console.log('listening on 3001')
});
When I debug the server.js file in VSCode, there is no error thrown and the code execution passes completely over lines 195 through 205 and ends on line 206, doesn't hit any breakpoints that I've set and I'm not sure why this is happening:
Again, I'm new to to the aforementioned technologies, but I would have expected at least an exception however I am not seeing one in VSCode Terminal or Output Window, which makes me wonder if there's something wrong with my connection string although I double checked with the book as well as examples online and it appears to be okay.
I should also point out that per the book I am following for this example, I am getting my connection string from Azure Portal here:


Connect to a Azure Cosmos DB for MongoDB database using Node.JS:
Below are the steps followed to connect Azure Cosmos DB for MongoDB using NodeJs:
The connection string known as the URI contains all the information required to establish a connection to your
Cosmos DBinstance.Client uses the URI to construct a new
MongoClientinstance.The line
await client.connect();makes an attempt to connect to the database. If successful, a message confirming the connection's success is printed. When an error happens, it is caught in the catch block, and a log entry is made.Created a search
queryto find records where theagefield contains a value greater than or equal to 28.The documents are fetched using the
.find(query)method, which then fetches them as an array using thetoArray()function.In
Catchblock errors are detected and logged with an error message in the event that they occur during the process.The process is initiated by the
main()function.Main()will catch and log any errors that are experienced while it is running.Code I tried with:
Output:
Data stored in DB.
Retrieved data using NodeJs.