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?
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.
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.
Meteor stores data in collections. To get started, declare a collection with new Mongo.
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({});
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With