Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

meteor

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?

like image 270
tadasajon Avatar asked May 27 '14 14:05

tadasajon


People also ask

How do I drop all collections in MongoDB?

To delete all documents in a collection, pass an empty document ({}). To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria.

How do I drop a collection in MongoDB Atlas?

The drop command removes the specified collection or view from the federated database instance storage configuration. Use the wildcard "*" to remove all collections generated by the wildcard collection function (that is, collectionName() ), including the wildcard collection rule itself.

What is Meteor in MongoDB?

Meteor stores data in collections. To get started, declare a collection with new Mongo.


1 Answers

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({});
        }
    }
});
like image 192
saimeunt Avatar answered Oct 04 '22 12:10

saimeunt