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't1 do fido.findByName
because findByName
would need to search through all documents and fido
is just a document.
1Well, 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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…