Antarr Byrd
Antarr Byrd

Reputation: 26061

can't remove records in mongodb

I'm trying to remove all the records in my mongodb collection. But when I check to see if it is empty all the records are still there.

var database = mongoose.connect('localhost','news');
Article.find(function(err,articles){
    Article.remove(articles);
});

Article.find(function(err,articles){
    if(!err){
        console.log(articles);
    }else{
        console.log(err);
    }
})
console.log(database);

Upvotes: 0

Views: 67

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311855

It's not working because remove takes a query conditions object, not the list of documents to remove. You also need to put your find inside your remove callback or it will be executed before the remove completes.

Try this instead:

Article.remove({}, function (err) {
    if (!err) {
        Article.find(function(err,articles){
            if(!err){
                console.log(articles);
            }else{
                console.log(err);
            }
        });
    }
});

Upvotes: 1

Related Questions