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 ...
随机推荐
- [批处理] Git中log的使用
1.获取两个提交之间的日志: git log SHA-1_A.. SHA-1_B--pretty=format:"%cd: %s" --date=format:%Y%m%d > ...
- bzoj2809 [Apio2012]dispatching(左偏树)
[Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 M ...
- 文件下载的ie11兼容性优化
在 http://www.cnblogs.com/sunshine6/p/8296945.html 中有说关于前后端分离时如何实现文件下载的功能,但是过完年回来,同事告诉我这个方式在ie11上存在不兼 ...
- 【记录】VMware解决网络找不到服务器的问题
本想在虚拟机上的Linux上练习安装Mysql8.0版本的,网络连不上的问题卡了N天简直 1. 点击虚拟机右键设置,虚拟机默认设置为NAT模式,这里无需修改. 2. 点击编辑,虚拟网络设置,勾选主机连 ...
- Elasticsearch NEST 控制字段名称命名格式
在使用NEST操作elasticsearch时,字段名会根据model中字段,默认为首字母小写. 如果需要调整NEST的默认明个规则,可以在 ConnectionSettings中进行自定义. var ...
- P4233 射命丸文的笔记
思路 题目要求求的是哈密顿回路的期望数量,实际上就是哈密顿回路的总数/有哈密顿回路的竞赛图的数量 n个点的所有竞赛图中哈密顿回路的总数为 \[ (n-1)! 2^{\frac{n(n-1)}{2}-n ...
- UVA11996 Jewel Magic
思路 splay维护序列的hash值即可 因为有rev操作,还要维护反串的hash值 代码 #include <cstdio> #include <cstring> #incl ...
- [minecraft]mcCoder制作有感
mcCoder是一个minecraft-forge-mod制作库,力图让mod制作者可以更简单的制作mod,减少mod制作者的mod制作难度. 在GitHub上关注这个项目: 原理 mcCoder主要 ...
- Ubuntu 下将 svg 图片转换为其他格式 (如 png)
参考 How to Convert SVG Files to other Image Formats on Ubuntu 12.04/11.10 Ubuntu 下将 svg 图片转换为其他格式 (如 ...
- centOS 及 ubuntu 下载地址记录
CentOS下载地址: http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1611.iso : ubu ...