connecting azure web app to azure cosmos db connecting azure web app to azure cosmos db mongoose mongoose

connecting azure web app to azure cosmos db


I recently completed the tutorial here that walks you through building an Express-based server that uses Mongoose to connect to Cosmos DB. You can see the end result here:https://github.com/ydogandjiev/react-cosmosdb

A couple of issues I ran into were:

  1. I had to use the following syntax to connect to the database (or escape the password string):

    mongoose.connect(`mongodb://${env.user}.documents.azure.com:${env.port}/?        ssl=true`, {  auth: {    user: env.user,    password: env.password  }});
  2. Using the latest version of the Mongo client requires you to enable the preview features on your CosmosDB instance:CosmosDB Preview Features


mongoose.connect wants the string to have a /DBNAME between the network part of the conn str and the options part.

mongoose.connect('mongodb://username:password@host:port/database?options...');

Take the connection string from the CosmosDB portal and insert the dbname with a slash /dbname before the ?ssl=true...

You can do this by setting COSMOSDB_CONNSTR to be just the part of the connection string up until but not including the ? and making sure it ends with a /

See http://mongoosejs.com/docs/connections.html


As I mentioned in my comments to Yuri Doganjiev answer, his code and the suggestion of enabling the preview features on the dashboard of azure cosmos db account resulted in succeeding in connecting to azure cosmosdb and create collection and querying the database. The version of mongoose used by me is 5.0.14.

The actual connection code used is below, where [host], [port], [username] and [password] are the value mentioned in connection string tab on azure dashboard. The [dbname] is the name of the database to which wish to connect.I did not escaped any special characters like (/, =, ?).Actually when I tried to escape some of them I got error messages.

mongoose.connect(`mongodb://[host]:[port]/[dbname]?ssl=true`, {    auth: {      user: `[username]`,      password: `[password]`    }  })  .then(() => console.log('connection successful'))  .catch((err) => console.error(err));

The problem for me was that I was not able to receive any error messages besides the generic one like 404 - not found or 500 - internal error problem.

Only after I have run npm start in the kudu console under the root directory of the app I started to receive meaningful error messages which helped me to identify were the problem is. Furthermore the console.log function used in the app code printed its message in the kudu console, showing if the connection is successful or displaying the newly inserted objects in the collection.

Another issue that I have encountered is that you cannot end the node process in kudu console by Ctrl + C. The only way to end the process that I found is the process explorer in kudu that is displaying the running processes and lets you kill them. So even when I have made changes to the code, such changes where not reflected in the browser immediately as the old node process (using the old code) was still running.

I do not know what kind of process manager uses Azure Web App but not always changes in the code are reflected in the browser.