Drake Guan
Drake Guan

Reputation: 15162

How do I use an existing MongoDB in a Meteor project?

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?

Upvotes: 85

Views: 71762

Answers (8)

Shaharyar
Shaharyar

Reputation: 12459

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'

Upvotes: 0

Milean
Milean

Reputation: 888

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.

Upvotes: 0

David Wihl
David Wihl

Reputation: 1491

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.

Upvotes: 9

danny
danny

Reputation: 10493

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.

Upvotes: 2

malix
malix

Reputation: 3582

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

Upvotes: 15

Dror
Dror

Reputation: 2420

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.

Upvotes: 157

pablo escobrah
pablo escobrah

Reputation: 79

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

Upvotes: -8

Josh Wulf
Josh Wulf

Reputation: 4877

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'.

Upvotes: 4

Related Questions