I want to know what is the difference between statics and methods in Mongoose
?
What I understood is that the methods are instances when creating a new object and the static ones are called without the need to instantiate or how they can be differentiated, I read the documentation but I did not find much information
usuarioEsquema.methods.generaToken = function () {
const user = this
console.log(user)
}
You are not that far away, however given you have a small doubt I will try to clarify it if possible.
METHOD
First of all, we are going to define what a method is, since in reality we are talking exclusively about them.
According to Wikipedia, a method is a subroutine or subprogram (you can call it a function if it seems clearer to you, although it does not necessarily return a value), which can be declared (belongs) both in a Class and in an Object . I assume you understand the difference between Class and Object.
If the method belongs to a class, it is known as a static method . A translation from Wikipedia could be:
This means that said method is not part of a class instance (it is not available in the class instance).
On the other hand, when a method belongs to an Object (instance of the class) it is known as an instance method . And it will only be available to be used by the created instance.
A static method can be called without creating an instance of the class. They get their name because they are resolved at compile time according to the Class to which they belong, being these constants or immutable (static), unlike instance methods that are resolved at runtime, being somehow dynamic (resolved by polymorphism according to the type of Object being instantiated).
Mongoose: Static Methods vs. Instance Methods
Now, to explain the differences between the two in Mongoose, we must first make clear the difference between Schema , Model and Document .
A Schema or Schema, is the definition of the elements or characteristics of a document within a Mongo Collection. Each Schema is related to a collection in the database, not to one document, but to the entire collection. It is a fingerprint of the documents that are stored in the collection.
A Model is a constructor compiled from a Schema definition. It allows us to create Documents associated with a MongoDB collection. Every Mongoose Model is compiled from a Schema .
A Document , on the other hand, is an instance of a Model and bears a direct relationship to a document stored in a collection in our Database.
Mongoose defines two types of methods: Static and Instance. Since a Document is an instance of a Model, we already have an idea of where each applies.
However, both families are declared as part of a Schema object .
Static methods belong to (or are declared) as part of
statics
the schema object (Schema.statics
). These methods can only be used directly from the ClassModel
. Each Model created from the Schema that contains the method will be able to use it. In Mongoose, when a static method is declared, the wordthis
(used inside the method) will refer to the Model.Instance methods belong to (or are declared) as part of
methods
the schema object (Schema.methods
). The methods declared in this way are only accessible (are available) from a Document. Every document created from the Model will have access to the instance method. In Mongoose, when an instance method is declared, the wordthis
(used inside the method) will refer to the Document.Clarified these points, it remains to say, when to use one or the other?
Well that will depend on what you need to do.
If you are dealing with operations that will require the use of specific properties of a document, you should use instance methods.
On the other hand, if it is about global operations on a collection of documents, it is appropriate to use a static method.
EXAMPLE
Let's try a practical example to finish and perhaps in this way we can understand even more the concepts raised here.
We will first create a collection in MongoDB called
animals
:Now using Mongoose from an Express application we can do the following:
As you can see in the code, two instance methods and two static methods have been created.
If we want to get all the animals that have a particular name, we must perform a search on the entire collection. That is why the method
findByName()
has been declared as static, since it is the Model that points to the collectionanimals
.If we want our cat to say
meow
, we'll use an instance method, since we're referring to a specific document of typecat
.We also see that if we wanted to count the number of horses in our collection, we could use a static method that performs the count over the entire collection. Of course there are methods included in Mongoose that perform such a task, such as
count()
orestimatedDocumentCount()
.If you run the above code, the output might look similar to the following:
I hope this helps you to clarify the doubt.