Using "meteor mongo" on localhost but with remote Database
Asked Answered
S

1

9

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.

Sold answered 26/2, 2014 at 22:29 Comment(0)
A
22

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'
      }
    });
  }
});
Abbyabbye answered 26/2, 2014 at 23:36 Comment(3)
Thanks David. Here is a use-case scenario that I just can't get my head around: I want to create security roles and store them in the DB ( Admin, Moderator, etc. ) and then assign those roles to specific users. Dealing with a local DB instance, vs. a remote DB, I'm just a little lost as to how to make this happen.Sold
Is there where I would make use of fixtures? Creating the accounts and roles when the server starts if they don't exist?Sold
I'm glad to be of help. I'd also recommend looking at the roles package.Abbyabbye

© 2022 - 2024 — McMap. All rights reserved.