What's the difference between a schema and model in mongoose?

I'm new to databases, and really to server side logic in general. I don't exactly understand the difference between schemas and models in mongoose, despite the fact i've read numerous explanations. Maybe someone here can explain it in a way that clicks with me. Thanks
8 Replies
ErickO
ErickO•4mo ago
dunno how much I can simplify it and not confuse you more but... A Schema is your blueprint, it tells you what your data should look like so that mongo (or mongoose) can check incoming data, look at the blueprint and go "this shouldn't go here, this should be a string not an int" The Model is just the way you interact with mongoose, remember mongoose is nothing but a middle-man between you and mongodb and the model is them being the middle-man, a model takes a schema and compiles an object that has all the methods and functions you need to interact with mongodb .insert() .update etc. in the background, every time you use one of those methods the model looks at the schema to make sure that the data you are giving it is the right one based on what you told it to look for
joey //
joey //•4mo ago
thanks .. so i got a dumb analogy if you could let me know if im on the right track ... so lets say a schema is like a set of rules as to how you should talk to someone ... and a model is the phone to communicate with them (to communicate with mongo db in this case) ... when we pass a schema to the model its like telling the model 'if the person doesnt talk to you in this specific manner , hang up" ..... that sounded utterly ridiculous but anyways
ErickO
ErickO•4mo ago
lmao, yes basically that
joey //
joey //•4mo ago
the model is like the middleman to speak with mongo , and the schema tells us how our communication should be formatted thanks lmao if i was as good at coding as i am at making obscene analogies id have it made
ErickO
ErickO•4mo ago
dumbing it down is a good way to learn tho, coding is full of fancy names and terms for what's honestly fairly simple concepts, but we just wanna sound smaht
Joao
Joao•4mo ago
Great analogy 😄
joey //
joey //•4mo ago
sarcasm by chance ? cause to be fair it was a pretty dumb analogy . if not, thanks
Joao
Joao•4mo ago
No, not all. It really is a great analogy. Schemas are there to enforce a contract, a set of rules. It's not that different than a social contract when talking to other people, even over the phone.
Want results from more Discord servers?
Add your server