5

I am creating a mongoose pre save hook, I want to know the name of the model/collection, which I am saving in the pre save hook. From which variable can I get it.

module.exports = exports = function lastModifiedPlugin (schema, options) {
    schema.pre('save', function (next) {
        var self = this;

        self.constructor.findOne({ _id: self._id }, function (err, launch){
            console.log(" model " + self.mongooseCollection);
            console.log(Object.getOwnPropertyNames(this));
            console.log(Object.getOwnPropertyNames(schema));

            next()
        });

    })

    if (options && options.index) {
        schema.path('lastMod').index(options.index)
    }
}

I tried to explore functions and properties of this and schema variable but could not got the name od model being saved.

Saurabh
  • 71,488
  • 40
  • 181
  • 244

1 Answers1

6

We can get it from:

self.constructor.modelName

Saurabh
  • 71,488
  • 40
  • 181
  • 244