Using “meteor mongo” on localhost but with remote Database

时光总嘲笑我的痴心妄想 提交于 2019-11-29 09:41:23

问题


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.


回答1:


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:PASSWORD@hatch.mongohq.com: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'
      }
    });
  }
});


来源:https://stackoverflow.com/questions/22054856/using-meteor-mongo-on-localhost-but-with-remote-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!