Fork me on GitHub


Methods and Statics

Each Schema can define instance and static methods for its model.


Methods are easy to define:

var AnimalSchema = new Schema({
    name: String
  , type: String

AnimalSchema.methods.findSimilarType = function findSimilarType (cb) {
  return this.model('Animal').find({ type: this.type }, cb);

Now when we have an instance of Animal we can call our findSimilarType method and find all animals with a matching type.

var Animal = mongoose.model('Animal', AnimalSchema);
var dog = new Animal({ name: 'Rover', type: 'dog' });

dog.findSimilarType(function (err, dogs) {
  if (err) return ...

Note that we return what .find() returns in our method. The advantages are two-fold. First, by passing cb into find we are making it optional b/c find called without a callback will not run the query. Secondly, this.find, this.where, and other Model methods return instances of Query which allow us to further utilize its expressive capabilities.

.where('name': /rover/i)
.exec(function (err, rovers) {
  if (err) ...


Statics are pretty much the same as methods but allow for defining functions that exist directly on your Model. = function search (name, cb) {
  return this.where('name', new RegExp(name, 'i')).exec(cb);
}'Rover', function (err) {
  if (err) ...