问题描述
我相信这个问题类似于这个问题 但术语不同.来自猫鼬 4 文档:
我们也可以定义自己的自定义文档实例方法.
//定义一个模式var AnimalSchema = new Schema({ name: String, type: String });//为我们的animalSchema 的methods"对象分配一个函数AnimalSchema.methods.findSimilarTypes = function (cb) {return this.model('Animal').find({ type: this.type }, cb);}
现在我们所有的动物实例都有一个可用的 findSimilarTypes 方法.
然后:
向模型添加静态方法也很简单.继续我们的动物架构:
//为我们的animalSchema 的statics"对象分配一个函数AnimalSchema.statics.findByName = 函数(名称,cb){return this.find({ name: new RegExp(name, 'i') }, cb);}var Animal = mongoose.model('Animal',animalSchema);Animal.findByName('fido', function (err, 动物) {控制台日志(动物);});
似乎使用静态方法,每个动物实例也可以使用 findByName
方法.Schema 中的 statics
和 methods
对象是什么?有什么区别,为什么我要使用一个而不是另一个?
statics
是在模型上定义的方法.methods
在文档(实例)上定义.
您可以使用静态方法,例如Animal.findByName
:
const fido = await Animal.findByName('fido');//fido =>{名称:'fido',类型:'狗'}
你可能会使用一个方法,比如fido.findSimilarTypes
:
const dog = await fido.findSimilarTypes();//狗 =>[ {name:'fido',type:'dog} , {name:'sheeba',type:'dog'} ]
但你不会做 Animals.findSimilarTypes()
因为 Animals 是一个模型,它没有类型".findSimilarTypes
需要一个在 Animals 模型中不存在的 this.type
,只有一个文档实例会包含模型中定义的属性.
同样,您也不会¹ 执行 fido.findByName
,因为 findByName
需要搜索所有文档,而 fido
只是 一个文档.
¹好吧,技术上你可以,因为实例确实可以访问集合(this.constructor
或 this.model('Animal')
) 但它拥有一个不使用实例中的任何属性的实例方法是没有意义的(至少在这种情况下).(感谢 @AaronDufour 指出这一点)>
I believe this question is similar to this one but the terminology is different. From the Mongoose 4 documentation:
// 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);
}
And then:
// 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);
});
It seems with static methods each of the animal instances would have the findByName
method available to it as well. What are the statics
and methods
objects in a Schema? What is the difference and why would I use one over the other?
statics
are the methods defined on the Model. methods
are defined on the document (instance).
You might use a static method like Animal.findByName
:
const fido = await Animal.findByName('fido');
// fido => { name: 'fido', type: 'dog' }
And you might use an instance method like fido.findSimilarTypes
:
const dogs = await fido.findSimilarTypes();
// dogs => [ {name:'fido',type:'dog} , {name:'sheeba',type:'dog'} ]
But you wouldn't do Animals.findSimilarTypes()
because Animals is a model, it has no "type". findSimilarTypes
needs a this.type
which wouldn't exist in Animals model, only a document instance would contain that property, as defined in the model.
Similarly you wouldn't¹ do fido.findByName
because findByName
would need to search through all documents and fido
is just a document.
¹Well, technically you can, because instance does have access to the collection (this.constructor
or this.model('Animal')
) but it wouldn't make sense (at least in this case) to have an instance method that doesn't use any properties from the instance. (thanks to @AaronDufour for pointing this out)
这篇关于猫鼬“静态"方法与“实例"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!