mongoose Mongoose Schemas Schema Statics


Example

Schema Statics are methods that can be invoked directly by a Model (unlike Schema Methods, which need to be invoked by an instance of a Mongoose document). You assign a Static to a schema by adding the function to the schema's statics object.

One example use case is for constructing custom queries:

userSchema.statics.findByName = function(name, callback) {
    return this.model.find({ name: name }, callback);
}

var User = mongoose.model('User', userSchema)

User.findByName('Kobe', function(err, documents) {
    console.log(documents)
})