statics are the methods defined on the Model.

methods are defined on the document (instance).

We may also define our own custom document instance methods too.

// define a schema
var animalSchema = new Schema({ name: String, type: String }); // assign a function to the "methods" object of our animalSchema
animalSchema.methods.findSimilarTypes = function (cb) {
return this.model('Animal').find({ type: this.type }, cb);
}

Now all of our animal document instances have a findSimilarTypes method available to it.

And then:

Adding static methods to a Model is simple as well. Continuing with our animalSchema:

// assign a function to the "statics" object of our animalSchema
animalSchema.statics.findByName = function (name, cb) {
return this.find({ name: new RegExp(name, 'i') }, cb);
} var Animal = mongoose.model('Animal', animalSchema);
Animal.findByName('fido', function (err, animals) {
console.log(animals);
});

You might do,

Animal .  Modal
Animal.findByName('fido', function(err, fido){
// fido => { name: 'fido', type: 'dog' }
});

And then you might use the document instance fido to do

fido .  document

fido.findSimilarTypes(function(err, dogs){
// dogs => [ {name:'fido',type:'dog} , {name:'sheeba',type:'dog'} ]
});

Mongoose 'static' methods vs. 'instance' methods的更多相关文章

  1. Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field

    一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...

  2. Instance Methods are Curried Functions in Swift

    An instance method in Swift is just a type method that takes the instance as an argument and returns ...

  3. Swift编程语言学习12 ——实例方法(Instance Methods)和类型方法(Type Methods)

    方法是与某些特定类型相关联的函数.类.结构体.枚举都能够定义实例方法:实例方法为给定类型的实例封装了详细的任务与功能.类.结构体.枚举也能够定义类型方法:类型方法与类型本身相关联.类型方法与 Obje ...

  4. UIView 实例方法 Instance Methods(转)

    好了,我接着上篇,开始我们的对UIView 实例方法的探索 UIView 实例方法 Instance Methods 初始化一个视图 - (id)initWithFrame:(CGRect)aRect ...

  5. Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】

    1.问题:    在dealloc方法中使用[self.xxx release]和[xxx release]的区别? 用Xcode的Analyze分析我的Project,会列出一堆如下的提示:Inco ...

  6. http methods & restful api methods

    http methods & restful api methods 超文本传输​​协议(HTTP)是用于传输超媒体文档(例如HTML)的应用层协议 https://developer.moz ...

  7. Static and Instance Methods in JavaScript

    class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...

  8. Android JNI 学习(八):Calling Instance Methods Api

    一.GetMethodID jmethodIDGetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig); 返回 ...

  9. ReferenceEquals()、static Equals() 、instance Equals() 与 operator==之间的联系与区别

    当你创建一个用户自定义类型(类或结构)时,你需要给你的类型定义相等操作.C#中提供了几个不同的函数来验证两个对象是否满足“相等”的含义.public static bool ReferenceEqua ...

随机推荐

  1. cshtml razor

    禁止转换字符 @(Html.Raw(@item.conent)) 三目运算 @(ViewBag.submitType==1?"blue":"")

  2. Flask实战-留言板-使用Flask-DebugToolbar调试程序、Flask配置的两种组织形式

    使用Flask-DebugToolbar调试程序 扩展Flask-DebugToolbar提供了一系列调试功能,可以用来查看请求的SQL语句.配置选项.资源加载情况等信息.这些信息在开发时会非常有用. ...

  3. 从centos镜像创建maven仓库

    创建镜像 1. 使用centos7作为基础镜像2. 将jdk1.8(官方要求1.8)和nexus3解压后的两个文件放进cp进去3. export环境变量后启动一下试一试4. docker commit ...

  4. 使用docker试用各种软件及docker-ES设置

    试用开源软件的优劣势 由于现在容器化的热度,大部分软件都有docker official镜像,那么使用docker就是试用软件很好的方法: 优势: 1.可以免去安装部署的过程. 2.不会对当前系统环境 ...

  5. Linux Spi驱动移植小结

    2012-01-07 22:21:29 效果图: 理论学习后,主要是linux中spi子系统设备框架的了解后,主控制器与设备分离的思想,那么我要开始动手了. 1,  make menuconfig添加 ...

  6. mysql在Windows下使用mysqldump命令手动备份数据库和自动备份数据库

    手动备份: cmd控制台: 先进入mysql所在的bin目录下,如:cd C:\Program Files\MySQL\MySQL Server 5.5\bin mysqldump -u root - ...

  7. Arrays工具类

    1.Arrays工具类针对数组进行操作的工具类 提供了排序查找等功能 2.方法: Arrays.toString(int[] a) 将数据转换成字符串 Arrays.sort(int[] a) 将数组 ...

  8. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)A Red Rover

    题目链接 大概意思就是,找出一个字串,将其所有出现的地方替换成一个字母M后的长度len1,然后再加上这个字串的长度t_len得到一个新的长度len,求这个len最小等于多少. 当然,也可以不找这个串, ...

  9. java导出excel,多表头合并

    要求结果图如下: 有空补充具体逻辑 参考:https://blog.csdn.net/dj0721/article/details/72463042 HSSFColor  背景颜色选择  参考:htt ...

  10. 制作用户登录界面(JAVA实现)

    设计实现如图所示的个人信息注册.包含单选按钮.多选按钮.下拉框事件. Zuoye类: package example02; import java.awt.event.ActionEvent; imp ...