85

Let's say there is a running MongoDB server for a GUI client (by wxPython) for a while.

How could I connect my new Meteor project to my already existing MongoDB?

3

8 Answers 8

156

Use the environment variable MONGO_URL. Something like:

export MONGO_URL=mongodb://localhost:27017/your_db

Replace your_db with meteor or whatever db you want to use.

6
  • 4
    I'm stuck. Can you explain how to Replace your_db with "meteor" Dec 27, 2013 at 17:02
  • 3
    note that this will NOT work on prod if deploying to meteor -- see stackoverflow.com/questions/21971036/…
    – maxko87
    Apr 29, 2014 at 18:55
  • 2
    @SurjithSM export MONGO_URL=mongodb://localhost:27017/my_database_name will put data in a database named my_database_name. He meant that if you want, you can do export MONGO_URL=mongodb://localhost:27017/meteor. Tought I would advise naming your database name the same as your project. Aug 25, 2014 at 20:25
  • 1
    Can it be done with some configuration file? Tried with settings.json and it won't work
    – Kostanos
    Jul 17, 2015 at 16:10
  • 1
    this crashed the heck out of my local app.
    – Deborah
    Jun 19, 2016 at 12:03
15

We use npm:

  • Create a package.json file with npm init, if you don't have one already.

  • Enter and modify the following line in that file (replacing all the <...>'s):

"scripts": {"meteor": "MONGO_URL=mongodb://<USER>:<PASSWORD>@<SERVER>:<PORT>/<DB> meteor"}
  • You can then start meteor with just npm run meteor
2
  • 'MONGO_URL' is not recognized as an internal or external command Jun 28, 2016 at 11:19
  • 1
    it's an environment variable, make sure there are no spaces around the = sign
    – malix
    Jun 28, 2016 at 14:37
9

In the comments to danny's answer Tom Wijsman recommends patching packages/mongo-livedata/mongo_driver.js, line 21. A better place is in app/meteor/run.js, line 460. This way the environment variable is still picked up if present, such as when running Meteor on Heroku. Just change the default hardcoded mongodb://127.0.0.1 to the location of your MongoDB server.

4

You can use db.copyDatabase to do this, with a caveat that there is a bug and you can't update the data in Meteor. See https://github.com/meteor/meteor/issues/61

If you're using the development version of Meteor, you can transfer data from a running MongoDB server by starting your Meteor app, then doing:

mongo --port 3002

This will connect you to the Meteor app's Mongo server. Now use db.copyDatabase like this:

db.copyDatabase('myappDatabase', 'meteor', 'localhost');

This will copy the database myappDatabase from a MongoDB server running on the standard port on localhost, to the Meteor app Mongo server. The database name the Meteor app uses is 'meteor'.

2

Just copy the data to the Meteor MongoDB database - no reason to try to hook Meteor up to the existing database and risk overwriting things.

Use mongoexport to dump your collections individually, then mongoimport to import the files into the database named meteor in the Meteor MongoDB instance. The Meteor MongoDB instance runs on port 3002 with bind_address 127.0.0.1, and the data files are in the Meteor project subdirectory .meteor/local/db.

See the documentation if you're not familiar with import/export in MongoDB.

5
  • Does that mean we should deliver mongodb hosting to meteor instead of hosting by other means?
    – Drake Guan
    May 15, 2012 at 3:34
  • 2
    @Drake: Unless you can adjust Meteor to work with your database. app/lib/mongo_runner.js decides how to launch it (you might not even require launching code, thus look into how to disable it). The connection to it seems to happen inside packages/mongo-livedata/mongo_driver.js, I think you can simply change the url parameter on line 21. If you want to know its value, insert console.log(url); on the line before that, restart Meteor and watch your Meteor output closely. Make sure you do indeed call Meteor once in your app for it to trigger... May 15, 2012 at 16:17
  • @Drake: If you want me to place my comment as an answer (if it worked for you), please let me know. May 15, 2012 at 16:19
  • @TomWijsman: I tried console.log(url); and got the info! I'll suggest you to make this as complete as a work-around answer cause I believe it does provide help to the community, or at least, people like me not familiar with meteor yet~
    – Drake Guan
    May 16, 2012 at 9:54
  • heck of a lot easier to use mongochef to copy and paste data, rather than the export/import commands. 3t.io/mongochef
    – ChatGPT
    Dec 1, 2016 at 9:13
0

All I did was add the IP of my Digital ocean droplet server, instead of localhost, and it worked:

env: {
      ROOT_URL: 'http://yourdomain.com',
      MONGO_URL: 'mongodb://104.236.24.66:27017/meteor',
      PORT: 3002,
    },

EDIT: use MUP to deploy your meteor projects: https://github.com/zodern/meteor-up

env: {
      ROOT_URL: 'https://www.example.com',
      MONGO_URL: 'mongodb://localhost/meteor',
    },

Mup uses Docker, and will "link" your 2 containers, thus hosting both the app and mongo on the same VM (server). Your mongoDB shouldn't be accessible from the public IP for security reasons.

2
  • where did you add this? Jan 3, 2018 at 12:01
  • it's actually bad practice to use the public IP of your VM for the mongoDB, im going to update my answer to use localhost.
    – Milean
    Jan 8, 2018 at 11:45
0

Spent a lot of time and found out that it requires quotes around the URL:

export MONGO_URL='mongodb://localhost/meteor'
export MONGO_OPLOG_URL='op log url'
-8

You have to keep your app running in one terminal window then open another and type "meteor mongo" and it should work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.