Using “meteor mongo” on localhost but with remote Database

前端 未结 1 1509
面向向阳花
面向向阳花 2020-12-30 10:28

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
1条回答
  •  囚心锁ツ
    2020-12-30 11:08

    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'
          }
        });
      }
    });
    

    0 讨论(0)
提交回复
热议问题