Do statics() methods apply only for a model and methods() for items in the model?

tdd

#1

Question

Do statics() methods apply only for a model and methods() for items in the model?

Answer

That is correct, methods created with static() only modify the model, for example in this lesson we see that we are to create a findMostExpensive method, this would be a static method because it needs to go through all the documents related to the models instantiated from the MagicItemSchema:

magicItemSchema.statics.findMostExpensive = function(...){...};

by assigning it as a static method in the schema, once we create a model derived from it, it will have the capacity to search the most expensive item.

In comparison, a method created with methods() will only apply to documents in the collection of that model.

magicItemSchema.methods.use = function(...){...};

We still assign it to our schema, but since it knows that is related to methods, it will only apply directly to one document. For example, getting our most expensive will return a document, if we want to use one of the items in that document, we can now use our method use():

const mostExpensive = MagicItem.findMostExpensive(); //will return a document

mostExpensive.use() // it will decrease the count of units for the amount left after using one.