Mongoose 'static' methods vs. 'instance' methods
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的更多相关文章
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- 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 ...
- Swift编程语言学习12 ——实例方法(Instance Methods)和类型方法(Type Methods)
方法是与某些特定类型相关联的函数.类.结构体.枚举都能够定义实例方法:实例方法为给定类型的实例封装了详细的任务与功能.类.结构体.枚举也能够定义类型方法:类型方法与类型本身相关联.类型方法与 Obje ...
- UIView 实例方法 Instance Methods(转)
好了,我接着上篇,开始我们的对UIView 实例方法的探索 UIView 实例方法 Instance Methods 初始化一个视图 - (id)initWithFrame:(CGRect)aRect ...
- Don’t Use Accessor Methods in Initializer Methods and dealloc 【初始化和dealloc方法中不要调用属性的存取方法,而要直接调用 _实例变量】
1.问题: 在dealloc方法中使用[self.xxx release]和[xxx release]的区别? 用Xcode的Analyze分析我的Project,会列出一堆如下的提示:Inco ...
- http methods & restful api methods
http methods & restful api methods 超文本传输协议(HTTP)是用于传输超媒体文档(例如HTML)的应用层协议 https://developer.moz ...
- Static and Instance Methods in JavaScript
class.method/instance method https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-jav ...
- Android JNI 学习(八):Calling Instance Methods Api
一.GetMethodID jmethodIDGetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig); 返回 ...
- ReferenceEquals()、static Equals() 、instance Equals() 与 operator==之间的联系与区别
当你创建一个用户自定义类型(类或结构)时,你需要给你的类型定义相等操作.C#中提供了几个不同的函数来验证两个对象是否满足“相等”的含义.public static bool ReferenceEqua ...
随机推荐
- tiny6410 烧写uboot 转载
#烧录 参考: 03- Tiny6410刷机指南.pdf 假设拿到的Tiny6410开发板没有提前下载任何程序,包括Bootloader. ##Bootloader - Superboot Super ...
- TTL集成门电路
一.TTL集成门电路的结构1.总体结构 所谓TTL就是transistor transistor logic,就是说是由晶体管和晶体管之间构成电路. 2. TTL集成门电路典型输入级形式 1)二 ...
- FileFilter过滤器
FileFilter过滤器原理: File对象的listFiles()方法做了三件事情: 第一件,遍历得到所有的文件/文件夹: 第二件,调用入参过滤器接口自己DIY的实现类中重写的accept()方法 ...
- centos7安装配置jdk
① java -version 可以查看系统自带的openjdk版本信息 ② rpm -qa | grep java 查看系统自带的Java文件 ③ 卸载文件(noarch文件可以不用删除) -- ...
- Cesium 学习笔记
Entity API 1,和 fill属性不太一样,outline没有对应的材质配置,而是用两个独立的属性outlineColor和outlineWidth. 注意outlineWidth属性仅仅在非 ...
- Jenkins安装及基本配置(Linux版,使用web容器 tomcat 搭建)
Jenkins是什么 Jenkins 是一个可扩展的持续集成引擎. 主要用于: 1 持续.自动地构建/测试软件项目. 2 监控一些定时执行的任务. Jenkins拥有的特性包括: ...
- bui前端框架+yii整理
这个是做bui前端样式整合的时候记录的. 首先当然是要下载一个yii的源码,搭建起来. 第一步将bui的样式迁移到yii的样式目录中去 这里我在样式外面加了一个bui的文件夹,表示这个文件夹中存放的是 ...
- 微信省市区 Mysql数据库
$jsonStr = '[{"cities":["\u5b89\u5e86","\u868c\u57e0","\u4eb3\u5d ...
- Excel导出采用mvc的ExcelResult继承遇到的问题Npoi导出
#region 构建Excel文档 //创建Excel文件的对象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSS ...
- 对Activity的DecorView的包装(二)
看了下公司的系统代码对于根布局decor_layout.xml的修改, 有所获. 前些时候才开始做系统开发的时候, 总想改改系统的源码, 至于原因: 人总是想装装, 在踩过几个别人修改的坑后, 还是觉 ...