Search code examples
javascriptmongodbmeteorhandlebars.jsmeteorite

Can't delete items from Mongo Database in Meteorjs


I have created an object in Meteorjs to hold the database info for N numbers of dice rolls, and then the dice are displayed using handlebars #each iterator. Here is some of my code:

The global Mongo collection:

Items = new Meteor.Collection('items');</code>

When I roll the dice, this is what happens to the collection when a button is clicked:

    //Don't want to bore you with all code, so here's just important parts...
    var randomNumber=Math.floor(Math.random() * numSides) +1);
    var numDice = 6;// It's really a variable passed in, but for here it's 6. 
    for (var i = 0; i < numDice; i++) {
              Items.insert(item: randomNumber)
    };

And then they display the info as dice because it displays numbers that I have CSS'd to look like Dice. But I'm straying here... Anyway, the dice rolling is awesome, but I want to clear the dice when I roll again. right now, they just keep adding up. And when I try to use any method to delete Mongo DB Items stuff, it crashes my app. Since I'm not sure how to debug very well yet in a browser, I need some help, and I'mm going to ask it here...

Now, my main problem is, when the dice are rolled again, I want to purge the database and start again. I am new to JavaScript and Meteor, and come from Java && Ruby land, so any suggestions are greatly appreciated.

I've tried

Items.removeIndexes(), 
Items.purge(), 
Items.remove({})

They all just freeze my app, and the numbers I displayed in the #each iterator are still there. I thought it would delete the stuff, and push the changes... No??? Please help.

The code is on GitHub @ http://www.github.com/rabbitfighter81/DMware/


Solution

  • You should use the remove method, but if you call it from the client, you can only remove one document per call, and the selector must refer to the documents _id field. So, here's an example to remove all the documents in a collection the client has:

    TheCollection.find().forEach(function(doc){
        TheCollection.remove(doc._id)
    })