Meteor: how can I drop all Mongo collections and clear all data on startup?

meteor, mongodb

Solution

Have you considered using `Meteor.startup` server-side ?

It allows you to register a callback that will get executed each time the server is (re)started.

Then you can use `MyCollection.remove({})` inside to wipe out everything.

The following piece of code clears every globally registered Meteor.Collection (ie using `MyCollection=new Meteor.Collection("collection-name")`) on every fresh start :

Meteor.startup(function(){
    var globalObject=Meteor.isClient?window:global;
    for(var property in globalObject){
        var object=globalObject[property];
        if(object instanceof Meteor.Collection){
            object.remove({});
        }
    }
});

Problem

I have a Meteor app which configures itself from a JSON API on startup. In order to properly coordinate all the clients, it builds a couple Mongo collections and stores data in them which clients then subscribe too. However, if the Meteor app is restarted, I would like it to wipe the database clean and re-configure itself from scratch. How can I get Meteor to drop all data and start from a clean slate each time the server code is restarted?

Original source