Skip to content

Instantly share code, notes, and snippets.

@travisjeffery
Created May 7, 2012 02:33
Show Gist options
  • Save travisjeffery/2625517 to your computer and use it in GitHub Desktop.
Save travisjeffery/2625517 to your computer and use it in GitHub Desktop.
Empty a Model's Collection With Mongoose
var mongoose = require("mongoose")
, assert = require("assert")
, async = require("async")
, Person
async.series([
function(callback){
mongoose.connect("mongodb://localhost/mydb")
mongoose.connection.on("open", callback)
},
function(callback){
Person = mongoose.model("Person", new mongoose.Schema({
name: String
, age: Number
}))
callback()
},
function(callback){
async.parallel([
function(callback){
(new Person({age: 21, name: "Travis"})).save(callback)
},
function(callback){
(new Person({age: 131, name: "Pablo"})).save(callback)
}
], callback)
},
function(callback){
Person.find({}, function(err, docs) {
assert.equal(2, docs.length)
callback()
})
},
function(callback){
Person.collection.remove()
callback()
},
function(callback){
Person.find({}, function(err, docs) {
assert.equal(0, docs.length)
callback()
})
}
], function(callback){
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment