除了数十个内建的Mongoose模型方法,我们还可以增加一些自定义的方法。 比如,当我们实现自定义实例方法buy()后,就可以调用practicalNodeBook文档的buy()方法。
bookSchema.method({
buy:
function(quantity, customer, callback){
var bookToPurchase =
this;
return callback(results);
},
refund:
function(customer, callback){
return callback(results);
}
});
当我们没有或不需要一个特定的文档对象的时候,静态方法就很有用:
bookSchema.
static({
getZeroInventoryReport:
function(callback){
return callback(books);
},
getCountOfBooksById:
function(bookId, callback){
return callback(count);
}
})