mongoose_fixtures.js
function insertCollection(modelName, data, db, callback) {
    callback = callback || {};
    
    //Counters for managing callbacks
    var tasks = { total: 0, done: 0 };
    
    //Load model
    var Model = db.model(modelName);
    
    //Clear existing collection
    Model.collection.remove(function(err) {
        if (err) return callback(err);
        
        //Convert object to array
        var items = [];
        if (Array.isArray(data)) {
            items = data;
        } else {
            for (var i in data) {
                items.push(data[i]);
            }
        }
        
        //Check number of tasks to run
        if (items.length == 0) {
            return callback();
        } else {
            tasks.total = items.length;
        }
        
        //Insert each item individually so we get Mongoose validation etc.
        items.forEach(function(item) {                       
            var doc = new Model(item);
            doc.save(function(err) {
                if (err) return callback(err);
                
                //Check if task queue is complete
                tasks.done++;
                if (tasks.done == tasks.total) callback();
            });
        });
    });
}
function insertCollection(modelName, data, db, callback) {
    callback = callback || {};
    
    //Counters for managing callbacks
    var tasks = { total: 0, done: 0 };
    
    //Load model
    var Model = db.model(modelName);
    
    //Clear existing collection
    Model.collection.remove(function(err) {
        if (err) return callback(err);
        
        //Convert object to array
        var items = [];
        if (Array.isArray(data)) {
            items = data;
        } else {
            for (var i in data) {
                items.push(data[i]);
            }
        }
        
        //Check number of tasks to run
        if (items.length == 0) {
            return callback();
        } else {
            tasks.total = items.length;
        }
        
        //Insert each item individually so we get Mongoose validation etc.
        items.forEach(function(item) {                       
            var doc = new Model(item);
            doc.save(function(err) {
                if (err) return callback(err);
                
                //Check if task queue is complete
                tasks.done++;
                if (tasks.done == tasks.total) callback();
            });
        });
    });
}
function insertCollection ( modelName , data , db , callback ) { callback = callback || { } ; //Counters for managing callbacks var tasks = { total : 0 , done : 0 } ; //Load model var Model = db . model ( modelName ) ; //Clear existing collection Model . collection . remove ( function ( err ) { if ( err ) return callback ( err ) ; //Convert object to array var items = [ ] ; if ( Array . isArray ( data ) ) { items = data ; } else { for ( var i in data ) { items . push ( data [ i ] ) ; } } //Check number of tasks to run if ( items . length == 0 ) { return callback ( ) ; } else { tasks . total = items . length ; } //Insert each item individually so we get Mongoose validation etc. items . forEach ( function ( item ) { var doc = new Model ( item ) ; doc . save ( function ( err ) { if ( err ) return callback ( err ) ; //Check if task queue is complete tasks . done ++ ; if ( tasks . done == tasks . total ) callback ( ) ; } ) ; } ) ; } ) ; }
Clears a collection and inserts the given data as new documents

@param {String}      The name of the model e.g. User, Post etc.
@param {Object}      The data to insert, as an array or object. E.g.:
{ user1: {name: 'Alex'}, user2: {name: 'Bob'} }
or:
[ {name: 'Alex'}, {name:'Bob'} ]
@param {Connection}  The mongoose connection to use
@param {Function}    Callback
Clears a collection and inserts the given data as new documents