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. sitecore8.2 基于站点来查询item

    树形结构: /sitecore /content /foo-site /home /my-account /bar-site /home /my-account /baz-site /home /my ...

  2. lotus domino 软件学习网站(自己收藏的)

    lotus domino 软件学习网站(自己收藏的) 我学习lotus domino时间也不是很长,相比较学习lotus,学习java的时间还是比较长的,刚开始看网上的说法都是不看好lotus的, 但 ...

  3. 菜鸟redis初学

    该随笔为本人自学redis所遇到的错误,写这些初衷完全是为了避免以后犯相同的错误,如果对别人有帮助,那就相互促进. 在Java中使用redis,首先你的Jdk要能运行,如果没配置好,网上有很多jdk环 ...

  4. 模板std::mutex用法:

    std::mutex mymutex; std::lock_guard<std::mutex> lock(mymutex);

  5. JAVA EE 第二周(XML简述以及web请求的过程)

    一. 对于XML,我分别从以下几个方面来简述: 1.定义: XML是一种可扩展的标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. (可扩展标记语言:可扩展标记语言是一 ...

  6. 利用h5 meta 头标签设置og属性进行帖子分享图片时而有时而无

    <meta property="og:title" content="fgsfg"> <meta property="og:desc ...

  7. 按下回车默认提交form表单问题

    最近开发中碰到一个问题,项目中有几个列表展示页面,允许用户通过查询条件模糊查询数据.用户录入关键字后点击回车会调用查询方法根据关键字查询,原先功能没有问题,但是最近发现在查询输入框中按下回车会直接刷新 ...

  8. PLS-00357: Table,View Or Sequence reference 'SEQ_TRADE_RECODE.NEXTVAL' not allowed in this context

    oracle数据库: 为了使ID自增,建了序列后,创建触发器: create or replace TRIGGER TRIG_INSERT_TRADE_RECODE BEFORE INSERT ON ...

  9. HBuilder --- MUI , HTML5

    一.创建简单app应用 ① ② ③连接手机 ④ 二.MUI  各组件的运用 http://dev.dcloud.net.cn/mui/snippet/ 三. HTML5plus 参考文档:  http ...

  10. 汇编笔记-DOSBox安装和使用(转载)

    我自己安装使用在Windwos10下面. [DOSBox简介] 1. 官方网址:http://www.dosbox.com/. 2. debug功能在win7之后系统已经不自带了,即64位系统是不能直 ...