Explain this custom mongoose schema method

var animalSchema = new Schema({ name: String, type: String });

animalSchema.methods.findSimilarTypes = function(cb) {
return this.model("Animal").find({ type: this.type }, cb);
};

var Animal = mongoose.model("Animal", animalSchema);
var dog = new Animal({ type: "dog" });

dog.findSimilarTypes(function(err, dogs) {
console.log(dogs); // woof
});

here you define a schema and then assign a method to findSimilarTypes. Why is it specified here as this.model ("Animal") instead of the Animal model defined later? What is the this here?

Feb.27,2021
Menu