Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor app on Modulus — how to reset database

I'm running a staged Meteor app on Modulus and would like to know if there's a way to do something similar to 'meteor reset' on the remote mongoDB.

I could use mongo's command line by running db.dropDatabase(); however this also removes system.users which contains the mongo database accounts.

Interested to know how this can be achieved in the flow of deploying.

like image 430
onepixelsolid Avatar asked Aug 13 '14 03:08

onepixelsolid


2 Answers

When you run meteor reset then meteor recursively removes all dirs and files from: .meteor/local.

  # source : meteor/tools/commands.js (line 806-807)
  ...
  var localDir = path.join(options.appDir, '.meteor', 'local');
  files.rm_recursive(localDir);
  ...

I understand that you would like to remove particular collections from database stored in MongoDB. There are few ways of doing that:

Remove collections from shell or mongo shell

Write script which loops over names of collections you want to drop and then execute db.getCollection(name).drop() on each one.

From cmd: mongo [database] --eval "db.getCollection([collectionName]).drop();"

or from mongo shell:

db.getCollection([collectionName]).drop();

Remove collections from MongoDB using Robomongo

This is straightforward method : click and remove.

Helpful note if one can connect to mongo's server with ssh:

If you have SSH access to server where is mongo then you can tunnel remote port Y to local port X, so mongo will be available locally on port X :

ssh -L27018:localhost:27017 user@host

Then in Robomongo you create connection to localhost:27018 and you have access to remote db.

Remove collections directly from Meteor App.

if(Meteor.isServer){
  Collection.remove({}) 
}

One of my production app removes some collections when new version is deployed:

 if(Meteor.isServer){
    Meteor.startup(function(){
      if(cleanDB){
        CollectionA.remove({});
        CollectionB.remove({});
        CollectionC.remove({});
      }
    })
 }
like image 166
Kuba Wyrobek Avatar answered Sep 23 '22 23:09

Kuba Wyrobek


If you'd like to use the MongoDB shell on Modulus:

db.getCollectionNames().forEach(function(name) {
  if (name.indexOf('system.') === -1) {
    db.getCollection(name).drop();
  }
})

Use the Robomongo app to connect and execute this, or on the command line:

mongo <dbname> --host proximus.modulusmongo.net:27017 --username <username> --password <password> --eval "db.getCollectionNames().forEach(function(name) {if (name.indexOf('system.') === -1) {db.getCollection(name).drop();}})"
like image 1
Aram Kocharyan Avatar answered Sep 21 '22 23:09

Aram Kocharyan