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?
Solution 1:
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:
We use npm
:
Create a
package.json
file withnpm 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:
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:
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:
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.