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

Meteor

Meteor Problem Overview


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?

Meteor Solutions


Solution 1 - Meteor

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.

Solution 2 - Meteor

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

Solution 3 - Meteor

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.

Solution 4 - Meteor

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

Solution 5 - Meteor

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.

Solution 6 - Meteor

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.

Solution 7 - Meteor

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'

Solution 8 - Meteor

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrake GuanView Question on Stackoverflow
Solution 1 - MeteorDrorView Answer on Stackoverflow
Solution 2 - MeteormalixView Answer on Stackoverflow
Solution 3 - MeteorDavid WihlView Answer on Stackoverflow
Solution 4 - MeteorJosh WulfView Answer on Stackoverflow
Solution 5 - MeteordannyView Answer on Stackoverflow
Solution 6 - MeteorMileanView Answer on Stackoverflow
Solution 7 - MeteorShaharyarView Answer on Stackoverflow
Solution 8 - Meteorpablo escobrahView Answer on Stackoverflow