MongoDB in the Cloud lecture out of date

So I tried creating an mLab account, but the only option I have is to create a MongoDB Atlas account which I did. mLab has been closed since February 2019 and is now apart of MongoDB. How ever It’s way different than what Mosh is using in his lecture.

Does anyone by any chance have a step by step tutorial on how to use MongoDB Atlas? Because I’ve never used it before.

1 Like

MLab add-on for Heroku was recently shut down. However, the good news is, using MongoDB atlas is simple too, and just needs a few additional steps.

  1. Create an account on MongoDB Cloud

  2. After providing some basic details, you’ll be asked to choose a cluster type. Lets go with Shared Clusters for now, since those are free.

  3. On the next screen, you can customize various settings such as cloud provider & region, cluster tier (M0 Sandbox is the only free one and provides 512mb storage) etc. When done, click on Create Cluster.

  4. On the navigation bar to the left, click on Database Access, then on Add New Database User.

  5. On the modal box that appears, choose Password as the authentication type, type in a new username and password, and select a role for the user. (Note: Role should be atleast Read and Write to any Database or else your app may not function properly)

  6. Again, on the navigation bar to the left, click on Network Access, then on Add IP Address.

  7. On the modal box that appears, click on Allow access from anywhere. (Note: For production apps, you should restrict the IP list)

  8. Return to the Clusters tab, click on connect, choose MongoDB compass, select v1.11 or earlier, and copy the connection string displayed.

  9. Copy the connection string as shown.
    a. You can paste the connection string into MongoDB compass to directly access and modify the online databse.
    b. You can paste this connection string in your node backend where connection to mongodb has been establisted, so that your backend starts using this online db instead of your local db.
    c. You could also set this string as an environment variable on Heroku so that node uses this db when running on heroku and your local db when running locally on your computer.

This was all about using mongoDB cloud with your app. For help deploying node.js to Heroku, here’s the official documentation.

Node.js Deployment Guide | Heroku Docs

5 Likes

Alright thanks @Spark . Though I did have to replace @ symbol with %40 in my connection string, otherwise it won’t connect to my MongoDB Compass. Did you have the same problem?

No but that’s because I didn’t use any special characters in the password. If you do, you’ll need to replace it with the url encoded version when typing it in compass. For example, as you said, %40 instead of @.

Hey @Spark,

What about “Setting Environment Variables on Heroku”? I still keep getting “application error”.

I followed Mosh’s instructions step by step, how ever There are 2 warnings I got after configuring the vidly_db:

‘ssl’ is not recognized as an internal or external command, operable program or batch file. ‘authSource’ is not recognized as an internal or external command, operable program or batch file.

I don’t know if that has anything to do with it or not.

Also I’ve been using my cluster connection:

I replaced password with the password I configured.

But what could I be missing?

What about “Setting Environment Variables on Heroku”? I still keep getting “application error”.

Sometimes, the config package that Mosh uses in his lecture does not work properly on heroku. Not sure why, but maybe due to the sandboxed nature of heroku slugs.

What you could instead do is, use process.env command to access the environment variables on heroku.

So, if your database connection string is stored in an environment variable named “dbConn”, the command to retrieve it inside nodejs would be process.env.dbConn".

Heroku automatically sets the node environment as “production”. You can access this using the command process.env.node_env command.

As for how to set the environment variables on heroku, log on to Heroku, open your app, go to the settings tab, and click on Reveal Config Vars. You can then add any key-value pair as an environment variable that will be exposed to your app and accessible through process.env command.

For the other errors, it’s most likely a problem with the code. Please post a screenshot of your terminal/console showing the error and the commands you typed before that. It would help if you could also share a link to the github repo. That way I could help you debug better.

Here are the errors and warnings I got:

ssl_auth_source

lf

The screenshot of heroku logs you’ve posted is not the actual error. You’ll find the actual error logs if you scroll up.

If previous logs aren’t available anymore, replicate the error, and then take a screenshot of the actual error logs.

Sorry that’s all I got from my application log. There’s nothing else.

Read the first line of the log

This is probably not a problem with NPM. There is likely additional logging output above.

Okay I got it now, it took a bit of time for it to load.

MongoNetworkError : Failed to connect to server

This indicates your node back end is unable to connect to the mongodb online database. The following might be the issues:

  1. Check if allow access from anywhere is enabled (steps 6 and 7 in the initial instructions I posted).
  2. Check the password. I’d also suggest you to use a password without special characters as there might be a parsing problem behind the scenes.
  3. If you’ve declared any config variables on Heroku, check if they are being accessed by node or not. You can do this using console.log(process.env.<var_name_here>), and then looking at the application log on heroku when the app loads.

If none of the above works, then there’s a problem with the code itself. Please share a GitHub link to your code or a screenshot of the files where the connection with mongodb atlas is specified. I’d be able to guide you much better that way.