如何在流星项目中使用现有 MongoDB?

假设有一个用于 GUI 客户机的 MongoDB 服务器(通过 WxPython)正在运行一段时间。

如何连接我的新流星项目到我的 已经存在的 MongoDB

70785 次浏览

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.

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

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.

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.

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

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

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.

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'