I need help connecting to mlab.com database and importing data

I’ve tried both methods suggested in setup docs applying it to my mongo.connect function and connection is failing

To connect using the mongo shell:
mongo ds113849.mlab.com:13849/mydb -u <dbuser> -p <dbpassword>
To connect using a driver via the standard MongoDB URI (what's this?):

mongodb://<dbuser>:<dbpassword>@ds113849.mlab.com:13849/mydb

What’s the exact code you’re using?

node src/connect.js

const mongo = require('mongodb').MongoClient
function doConnect() {
 	var url = 'mongodb://<username>:<password>@ds113849.mlab.com:13849/mydb'
	mongo.connect(url, (err, db) => {
		if(err) { 
			throw err
		} else {
			console.log('Successfully connected to MongoDB')
		}
	})
}
doConnect()

<username> and <password> are supposed to be replaced with the actual username and password (URI encoded if necessary).

Edit: Note that they should not be hard coded, but instead stored in environment variables in the .env file.

Example:
.env file:

USERNAME=harrypotter
PASSWORD=avadakedavra

Your code:

const url = `mongodb://${process.env.USERNAME}:${process.env.PASSWORD}@ds113849.mlab.com:13849/mydb`;

I did that for security reasons. I am well aware how to replace placeholders in code.

What exactly is happening? You’re getting an error? Is anything displaying in the console?

Trying to use Mongoose instead as recommended by a back end dev to get around the problem running
npm install mongoose and running connect.js again with node
:

//  connect.js
const mongoose = require('mongoose')
process.env.MONGODB == mongodb://<username>:<password>@ds113849.mlab.com:13849/mydb
mongoose.connect(process.env.MONGODB);
mongoose.connection.on('error', function () {
  console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  process.exit(1);
});

What happens when you use Mongoose? Same problem?

Did you make sure that you setup a user for that particular database you are using? Check under the users tab and make sure you have a user listed and you are using the username and password assigned to it.

Thanks for your reply. I had created a user yes before using Mongoose to connect. I was successful when I tried a code example on a blog post titled Mongoose Connection best practice under the heading Managing the Mongoose connection. That’s all very well but I still need to figure out how to import JSON objects representing social media posts and metadata into a collection.

Good news! I figured out to use mlab tools to import/export a collection from adding documents into a collection I named feed to match my app.

Import collection

mongoimport -h ds113849.mlab.com:13849 -d mydb -c feed -u <user> -p <password> --file <input file>

Export collection

mongoexport -h ds113849.mlab.com:13849 -d mydb -c feed -u <user> -p <password> -o feed.json

You get a set of objects dumped into the file but without an array to map() over:


```{"_id":{"$oid":"5aaba1f5734d1d1b8288625d"},"user":"Michael Bay","likes":0,"avatar":"male.svg","action":"replied","images":[],"photosAmount":0,"profile":"","comments":"cool","when":"1519568559"}```