jazz
jazz

Reputation: 3

On Schema , Model and Mongoose

I am trying to understand the Schema, Model and Mongoose based on their type. I am really confused regarding is Schema a class or just look similar to class? Is Model a class or just behave like class in some aspects? Are they just function constructors? Based on some reading I have theorized my understanding as shown below. I want to get it checked.

const personSchema = new mongoose.Schema({
    name: String,
    age: Number
});

Now in this code snippet I think personSchema is an instance of the class Schema. Is Schema a class in here?

Now constrast this with the below code snippet.

function Person(name, age) {
    this.name = name;
    this.age = age;
}

const person1 = new Person('John', 30);

So personSchema can be understand here as similiar to the person1 in the latter code snippet which will just make Schema a class. In the second code snippet we are passing values as 'John' and '30' while instantiating the Person function constructor and in the first code snippet we are passing an object

{
    name: String,
    age: Number
}

This gives us our schema. So what I understand is that Mongoose must have an Schema function constructor just like we have a Person function constructor here and the personSchema is an instance of the Schema function constructor just like the person1 is an instance of the Person function constructor.

Now coming to Models.

const Person = mongoose.model('Person', personSchema);
const person1 = new Person({ name: 'John', age: 30 });

In the above code snippet I am confused about the line const Person = mongoose.model('Person', personSchema); If I think of model as some function constructor provided by Mongoose then it will make Person an instance of that function constructor but to instantiate a function constructor we should use new keyword which we are clearly not using here. If we draw a constrast with the below example:

function Person(name, age) {
    this.name = name;
    this.age = age;
}

const person1 = new Person('John', 30);

I can make a constrast that model must be the function constructor provided by the Mongoose. It draws a parallel with the Person function constructor. But the Person function constructor is instantitated with the line const person1 = new Person('John', 30); where new keyword is used and person1 is the instance of the function constructor Person but in our line const Person = mongoose.model('Person', personSchema); const person1 = new Person({ name: 'John', age: 30 }); what is Person in here and what is model in here? I can see the person1 is an instance created which is evidently an instance of the Person making Person a function constructor. My confusion is about the line const Person = mongoose.model('Person', personSchema); what is Person doing here and we are not using new keyword in here so how can Person be made an instance of the mongoose.model?

You can also give a better example and make me understand the concepts of what mongoose, model, Schema are internally implemented as what do they give result as to what are the instances made of Schema and model as apparently both are function constructor and behave like class/function constructor.

Upvotes: 0

Views: 25

Answers (0)

Related Questions