Before setting up the schema of your model install mongoose with npm first.
And as an example, I’m going to use one of my previous ruby on rails project’s database schema as an example with stocks trading.
first I’m going to create 2 files in our models folder
Set up my Company Schema then export the model with mongoose, with build-in validation of uniqueness.
Set up my User Schema
we set up nested values with name, and we remove the watchList from the previous post SQL structure because we can directly save the company inside each individual user with array type of data, to save multiple companies.
This is also the biggest difference between SQL and NoSQL, data is more dynamic. For validation instead of the build-in one by mongoose, we can also set up our own logic by using a function.
required: function() {
return this.name.length > 3;
}
You can learn more about validation in mongoose from the previous document.
So this is the basic about creating Schema of mongoose models with Node.js.