In the last video we spoke about some document collections we had said that there is a very good reason
for having a separate user collection and a separate survey collection.
So we're now going to flip back over to our code editor and we're going to set up this recipient subcollection
and nested within a single survey.
So going to change back over to my code editor inside of my model's directory I'm going to make a new
file and I'm going to call it recipient.
J.S. now inside of here we are going to create our sub document collection by first creating a schema
file or a schema object that represents a recipient.
So remember that we had said that every single recipient should have two properties associated with
it.
They should have the email address and the click property clicked will be a boolean.
That's either true or false depending upon whether or not this particular email address has received
or has actually been used to click on a link previously.
And remember this is all about somehow prigged preventing duplicate clicks on individual buttons by
our users.
OK.
So back inside of recipient Yes.
The initial setup of this schema is going to look very similar to how we previously set up models before.
So we'll say Konst Mongoose is required mongoose will pull off the schema object
and then we will use that schema object to make a recipient schema like so.
Now again inside of here we really just want to reflect the user's email address and whether or not
they've clicked on it before.
So we're going to give it an email property of string and then a responded property.
So I'm going to call those responded essentially you know clicked responded is obviously a lot more
clear what it means rather than clicked.
So responded means.
Has this person responded to the survey yet.
Yes or no.
So we want this to be of type Boolean and we are going to default it's value to be false.
So by default whenever surveys first created no they have not clicked on anything.
And until you actually click on yes or no will we ever flip this respondent property over to true OK.
So this is how we set up the initial definition of what our sub document looks like.
So we're saying that we have a sub document as email and responded.
And now at the bottom this is where things are going to be a little bit different.
Rather than registering the schema with Mongoose we are going to export the schema.
So I'll say module exports recipient schema.
And again please check your spelling on recipients.
Very easy to make a typo here.
OK.
So this now sets up the idea of a recipient.
So we know what a recipient looks like.
We are now going to import this schema into our survey.
JS file.
And then we will sociate it with the survey model.
So we're going to change back over to survey geus at the top we will import recipient schema
from dot slash recipient.
And then finally we're going to use the recipient schema to set up the actual collection or some document
collection inside of our model.
So right here we still have the list of recipients.
When we had said this was going to be an array of strings.
Well now rather than being an array of strings we're going to say it's going to be an array or a list
of records that conform to the recipient schema.
So rather than saying recipients is an array of strings it's going to be an array of recipient schema
like so.
OK.
So now whenever mongoose first loads up the survey's model whenever it ever saves a record to the survey's
collection it will look at the schema right here.
They'll say OK it's got a title it's got a body it's got to subject whatever.
And then we'll get to recipients and we'll say Ooh interesting.
This is going to be an array of recipient schema records.
So every record or every object inside this array right here must obey the schema that we defined over
here.
So that's pretty much it.
That's how we load up a sub or how we create a sub document collection.
Now of course we still need to see how we add records to this sub document collection all that kind
of good stuff.
But this is at least good enough for understanding how we create it in the first place.
OK.
So this is looking pretty good.
We've now got our sub document collection set up.
Let's now continue in the next section.