Skip to content

扩展静态方法

多个里面查一个扩展静态

js
StudentSchema.statics.findByName = function (name) {
  // this => Student

  return this.findOne({ name })
}

await Student.findByName('foo')

扩展实例方法

只知道一个,要去数据库找其它的

js
StudentSchema.methods.findByName = function (name) {
  return this.model('Student').findOne({ name })
}

await new Student({ name: 'foo' }).findByName