Using "meteor mongo" on localhost but with remote Database

user1447679 picture user1447679 · Feb 26, 2014 · Viewed 13k times · Source

I'm following the telescope tutorial.

  1. I created a /client/collections/myfile.js
  2. I'm on localhost, but I'm launching Meteor with remote DB hosted on MongoHQ instead of using Meteor's local DB.
  3. In this tutorial I'm told to insert a new post by opening the Mongo console.

     $ meteor mongo
    

How can I:

$ meteor mongo (somehow connect to my remote DB to use the meteor commands in terminal

So that I can:

$ db.collectionname.insert({ stuff });

Or does this have nothing to do with "Meteor" in this case and I just use a Mongo shell outside of Meteor? The collection that I created in "/client/collections/collection.js" is this simply for telling Meteor which collection to push as a subset to the client?

I'd like to use the same DB ( remotely hosted with MongoHQ) for my localhost development, and my actual live dev.mysite.com so when I deploy to this dev site, anything I've done in the DB is also there and ready to go.

Answer

David Weldon picture David Weldon · Feb 27, 2014

Assuming you had a username of username, a password of PASSWORD, a database named test, and a hostname of hatch.mongohq.com:

Connecting via the shell

$ mongo hatch.mongohq.com:27017/test -u username -p PASSWORD

Connecting via Meteor

$ MONGO_URL="mongodb://username:[email protected]:27017/test" meteor

Other notes

  1. You should define your Meteor collections outside of the client directory so they can be used on both the client and the server. See this for more details.

  2. You will find that connecting to a remote database is much slower than connecting locally, so it's generally not recommended for development.

  3. Meteor creates a dev database for you when it starts. This also affords you the very helpful commands: meteor reset and meteor mongo, to reset, and connect to said database.


Initializing your database

Create a file on the server for initialization - e.g. server/initialize.js. When the server starts you can add users or other documents which do not yet exist. For example:

Meteor.startup(function() {
  if (Meteor.users.find().count() === 0) {
    Accounts.createUser({
      username: 'jsmith',
      password: 'password',
      profile: {
        firstName: 'John',
        lastName: 'Smith'
      }
    });
  }
});