Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to external MongoDB instance in Meteor?

Tags:

mongodb

meteor

I would like to find out how to connect to an external MongoDB instance in Meteor.

I have added this environment

Meteor.startup(function () { 
process.env.MONGO_URL = 'mongodb://[UN]:PW]@[host]:[port]/meteorTest'
});

but still the data is coming from the local database.

I want to move all the collections from my local db to this external db. I read all the tutorials, its all telling me to setup this evn variable but nothing really working. How do I test whether its connected or not?

like image 837
rahul cp Avatar asked Aug 02 '16 06:08

rahul cp


People also ask

How does MongoDB connect to local instance?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.

What is Meteor in MongoDB?

Meteor provides a complete open source platform for building web and mobile apps in pure JavaScript. The Meteor team chose MongoDB as its datastore for its performance, scalability, and rich features for JSON. Meteor apps run using JavaScript via Node. JS on the server and JavaScript in the phone's browser.


Video Answer


2 Answers

I don't like to use big repeating command and I was searching for a solution where I will be setting a variable embedded with something so every time I start my meteor app; the MONGO_URL will set to environment automatically. So this what I did:

In the package.json file I replaced the start parameter as below:

"scripts": {
    "start": "MONGO_URL=mongodb://username:password@host_url:portnumber/dbname meteor run"
  },

Now every time I want to run my app; I run npm start instead of meteor or meteor run

Note: there is a disadvantage with that. Your db credentials will be exposed if you put your db credentials to package.json file and add this file to version control.

like image 50
Abdul Avatar answered Nov 07 '22 09:11

Abdul


In my own experience; I have needed to set the environment variable before starting the meteorjs server app. To do this you will need to pass the environment variable on the command-line as you invoke meteor or preset the environment for the profile that is running the meteor app on your system.

So you would start your app with this kind of a command:

MONGO_URL='mongodb://user:[email protected]:12345/' meteor

You should also make sure that the mongodb is reachable and that your user credentials are correct! I am assuming you are trying to run meteor on your local machine using a remote mongodb instance.

On Windows

You will have to create a batch file in your meteor application folder to invoke the environment variable. There is an example of this here: https://stackoverflow.com/a/29833177/1997579

like image 25
Katalyst Avatar answered Nov 07 '22 07:11

Katalyst