In the last section we started writing a query that would not only find but also update the appropriate
survey in one entire ago.
So we essentially will end up just running this operation right here.
The correct surveys should be found and then correctly updated based on the criteria that we're going
to pass in this second object right here.
And so that's where we kind of left things off.
We need to figure out exactly how we are going to update the appropriate survey like this one that we
find right here.
Now when the previous code that we were just looking at at the previous kind of iteration of this query
that I was saying was really bad.
You'll notice that there was essentially two big things that we wanted to do over here.
The first thing we wanted to do was make sure that we updated the appropriate recipients responded propertied
true.
And then we wanted to increment the number of answers or the number of responses either the yes property
or the no property by exactly one.
And then we also set the last responded property as well.
So right now let's just kind of forget this last responded property for just a minute because we didn't
really care.
You know this isn't the most critical property in the world.
This was just a property that gave a little bit of information to our users to tell them hey here's
the last time someone voted on a particular survey.
So I want to just focus on these two properties right here.
I just want to update the number of responses and I want to update the given recipient to have responded
property of true.
So back over at our query that we're in the process of writing I'm going to find the second object right
here and inside of here we're going to place the logic that is going to update the respondent property
on the recipient and to give an answer or then the given choice of yes or no.
So let's take care of that choice property first.
All right.
Now this is going to be a little bit crazy.
We're going to write out the code for it and we'll talk about exactly what it's doing.
So inside of this second object I'm going to write dollar sign.
I can see that's going to be an object that has brackets square brackets mind you choice Colan one like
so.
So triple check your typing make sure you've got exactly the same code right here.
Now before we talk about what's going on I want to write up at the top remember that our choice property
is always going to be either yes or no.
So just remind our going to places up here just remind you what the choice property is going to be.
All right.
So let's talk about what is going on right here.
This dollar sign I see thing is called a mongo operator.
It allows us to put together some kind of intelligent or slightly intelligent logic inside of a query
that we issue to our database.
So this says find the choice property and remember remember choice is going to be either yes or no.
We'll talk about what these spring spare rocket or square brackets excuse me I mean in just a second.
So essentially find either the property or the no property and increment so I can see stands for increment
it by exactly 1.
So in other words provide one vote to either the yes or the no property on the given survey that was
just found.
Now of course we want to allow the decision of whether or not we are incrementing the yes or the no
property to be determined on the fly.
So in order to kind of calculate the key to use inside this object right here we're going to use a little
bit of yes 20:16 syntax called qi interpellation.
So if we put on here square brackets choice like so this does not create an array.
Let me be really clear about that.
It does not create an array.
Instead when this object right here is the value evaluated by the javascript runtime it's going to say
OK what's the value of the choice variable.
If the value of choice is yes then it's going to replace this thing right here with yes.
Otherwise if it's no it's going to replace it with no.
So in other words we are saying we don't really know at the time that we are writing the code whether
choice is going to be yes or no.
So only when we are kind of trying to actually update the given survey only after we have an actual
choice assigned will we actually figure out what this key right or should be in total.
Again this little block right here says increment either yes or no by exactly 1 on the survey record
that was found.
OK.
So that clearly takes care of step 1.
We are incrementing the appropriate feedback property by one.
Now the next thing we have to do is a little bit more complicated.
Believe it or not the next thing we have to do is make sure that we take the recipient who has found
and we update their responded property to True.
So on this one we're just going to write out the code for applying this update and we'll talk about
exactly what's going on.
So after this dollar sign Inc.
And this object direct here we'll put a comma and then the next line down.
So this is another key value property inside the second object right here.
We're going to say Dollar Sign set we're going to pass in an object.
And this is where things get weird.
We're you give it a key inside of a string of recipients dot dollars dollar sign dot responded are going
to say that's going to be true like so.
OK.
So I think that in your head you understand more or less what this line is meant to accomplish.
It is meant to update this responded property to True.
But the real question is how does this kind of modifier actually work how does this actually update
that property.
So we're doing here is we are making use of another Mongo operator.
We are saying that we want to set or update one of the properties inside of the survey record that was
found by this initial query right here.
So we are saying in the survey that was found look at the recipient's sub document collection inside
that Subotnick collection.
There are a bunch of different records to make sure that we are updating just the recipient that we
really care about.
We place dollar sign right here this dollar sign lines up with the dollar element or Elham match from
the original query that was issued up here.
So remember this line right here is going to find just the sub document collection recipient that we
actually care about.
And so maybe they are at index 500 inside of our list of recipients.
So that 500 of an index is then used right here in place of the dollar sign.
So you can imagine this thing is saying go into some documents collection find the appropriate recipient
who was just found in the original query right here.
Look at the respondent property and set that property to now be true rather than false like it was before.
And that's pretty much it.
So I know this clearly has we've written it out is really confusing.
It's really crazy.
But I want to give you a very intense a really in-depth query that you might find yourself writing when
you are making use of Mongo.
So let's walk through the entire thing again.
We are saying look at the survey Compare can find an update exactly one record inside that collection
we want to find the survey with this given ID who has a recipient with a given email and has not yet
responded to the survey.
After this one singular survey has been found make this following update to it increment the choice
either yes or no by one and update the recipient who we just found in the original query update the
respondent property to True because they have now actually responded so it's no longer responding false.
It is now responded true.
So that's very much the query interval.
So this syntax right here.
Yeah it's kind of nasty and you're probably sitting there thinking how in the world would I ever figure
out how to write this query myself.
And you know it's just something that comes with a little bit of experience.
Again I'll give you at least a little bit of a toolset or just a very quick couple of tips on exactly
how you can approach queries like this and figure out how to write them yourself.
But the end of the day what I want you to really focus on is the fact that when we issue this query
right here the entire thing from find the correct survey and then updating the survey is executed entirely
inside of our Mongo database.
So we are never going to exchange any information whatsoever from our express server back over to Mago
DB.
There's no communication there of the actual data.
We are just sending one instruction over to Mongo and saying hey make this update happen like just do
the thing Mungo's going to process this update and that's it.
We never have to go through the very laborious process of fetching the record pulling over the whole
as the recipients searching through them.
Updating one recipient and then saving back the entire record which in reality is a very long running
process.
OK so I think that we probably railed on about this query long enough.
I think that you probably understand a couple of the considerations around it.
So at this point let's take a quick break.
We're going to continue in the next section and when we do we're going to move this query that we just
wrote right here back into our survey routes J.S. file and so we will make sure that for every event
in our events array we attempt to actually run and apply this query right here.
So quick break and I'll see you in just a minute.