I want to define a method on my model that involves searching the documents of the same model, here is what I tried:
var mongoose = require('mongoose');
var Author = require('./author.js');
var bookSchema = mongoose.Schema({
author : { type: mongoose.Schema.Types.ObjectId, ref: 'author' },
genre: String,
});
bookSchema.methods.findSimilar = function(callback) {
bookSchema.find({'genre': this.genre}).exec(function doThings(err, doc){
/* ... */
});
};
module.exports = mongoose.model('book', bookSchema, 'book');
However, I get TypeError: bookSchema.find is not a function
.
I also tried bookSchema.methods.find()
, same result. How can I fix this?
Thanks,
Edit:
Inspired by this answer, I also tried this.model('Book').find()
, but I get a similar error: TypeError: this.model is not a function