I have a common method for updating document of any collection in MongoDB?
The following code is in file name Deleter.js
module.exports
I try to avoid globals as much as possible, since everything is by reference, and things can get messy. My solution
model.js
try {
if (mongoose.model('collectionName')) return mongoose.model('collectionName');
} catch(e) {
if (e.name === 'MissingSchemaError') {
var schema = new mongoose.Schema({ name: 'abc });
return mongoose.model('collectionName', schema);
}
}