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?

mitchken
  • 790
  • 9
  • 26
Drake Guan
  • 14,514
  • 15
  • 67
  • 94
  • Dror (http://stackoverflow.com/users/460278/dror) has the correct answer below: http://stackoverflow.com/a/12996674/1114274 – Mike Graf Oct 16 '13 at 18:28
  • Thanks for comment. I know @Dror's solution is much official instead of a hack, but I have already assigned an answer before. I'm not quite sure if it is a good and allowable behavior to change/update "answer" then? – Drake Guan Oct 17 '13 at 03:42
  • 1
    Thanks a lot for pointing me this. I'm more confident now. – Drake Guan Oct 18 '13 at 01:21

8 Answers8

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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Dror
  • 2,370
  • 1
  • 18
  • 13
  • 4
    I'm stuck. Can you explain how to Replace your_db with "meteor" – Surjith S M Dec 27 '13 at 17:02
  • 3
    note that this will NOT work on prod if deploying to meteor -- see http://stackoverflow.com/questions/21971036/mongodb-meteor-export-mongo-url-to-deployed-applications – maxko87 Apr 29 '14 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. – Benjamin Crouzier Aug 25 '14 at 20:25
  • 1
    Can it be done with some configuration file? Tried with settings.json and it won't work – Kostanos Jul 17 '15 at 16:10
  • 1
    this crashed the heck out of my local app. – Deborah Jun 19 '16 at 12:03
  • Be AWARE: The collections may look empty, you would need to make an insert in order to see the data. – Ruben Aug 08 '17 at 05:02
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
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
malix
  • 3,566
  • 1
  • 31
  • 41
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.

Community
  • 1
  • 1
David Wihl
  • 1,491
  • 13
  • 14
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'.

Josh Wulf
  • 4,727
  • 2
  • 20
  • 34
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
danny
  • 10,103
  • 10
  • 50
  • 57
  • Does that mean we should deliver mongodb hosting to meteor instead of hosting by other means? – Drake Guan May 15 '12 at 03: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... – Tamara Wijsman May 15 '12 at 16:17
  • @Drake: If you want me to place my comment as an answer (if it worked for you), please let me know. – Tamara Wijsman May 15 '12 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 '12 at 09:54
  • heck of a lot easier to use mongochef to copy and paste data, rather than the export/import commands. http://3t.io/mongochef/ – ChatGPT Dec 01 '16 at 09: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.

Milean
  • 878
  • 9
  • 16
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'
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
-8

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