In the last section we looked at an example of how to update a survey that I would call kind of a bad
approach or an inefficient approach.
It would have definitely worked out but it would have been doing a lot more work than we actually needed
to do between the Express server and our Mago database.
We're now going to continue by figuring out how to write a far more compact query that is going to find
an update just the survey that we really care about.
So let's get to it.
Now the first thing I want to do is I want to remind you once again that after we process all of our
different events coming from grid we end up with an object that looks like this right here.
So we've got survey ID email and the choice properties.
Now I want to kind of look at this object right here.
I want to think about how all of our actual surveys look inside of our database and then figure out
if we can't point out like a couple of different properties from this subject right here and compare
it to some of the different properties that we want to actually look up inside of our data.
In other words given an object like this right here what properties do I need to look at on my survey
records in my Mongo database in my surveys collection to make sure that I am finding the exactly correct
survey.
All right.
So let's take a look at this.
So we're going to say given any event of this right here.
So a survey of 4 5 6 an email of a at a dot com and a choice of yes exactly what records inside of my
database do I want to update.
So I've listed out two possible records that I've gotten side of my surveys collection inside of Mongo.
So you can see that every survey has an ID.
The sub document collection of recipients and the yes and no properties over here as well.
Now of course the survey has many other records as well.
But right now these are kind of the only properties that we really care about as far as kind of finding
and updating the correct survey goes.
So let's walk through the logic of exactly what we really care about inside of our query to make sure
that we find and update only the surveys that we really truly care about.
So we're going to look at this property by property.
We're going to say first off OK of course we want to find the survey with a given ID like that's the
guarantee.
Without a doubt we want to find the survey with a given ID.
So when I look at this first survey right here I see that it has an I.D. of 4 of 1 2 3 which clearly
does not match up to 4 5 6.
So pretty much from the get go we know without a doubt we don't care about this survey right here.
We don't care about it so we're going to kind of throw it out the window.
So let's go down to the next survey.
And again we are pretending that these are both records inside of our service collection.
Now this survey Yeah it definitely has an idea of 4 5 6.
Now here's the trick.
Before we just say oh OK it has a matching survey.
This must be the one that we want to update.
This must be the one that we want to care about.
That's not exactly what we're going to do.
We're going to write a query where we say find a survey that has not only the correct I.D. but also
has a recipient with this given e-mail and has not yet responded or has responded property of false.
So in other words I want to find not just this survey because it has an idea of four or five six but
I want to make sure that this survey right here also has a record in its recipient some document collection
with an email of a at a dot.com.
And I want to make sure that that recipient has responded false to our given surveys so they've never
given feedback before.
So I would want to look into this recipients of document collection.
I would say OK here's a recipient they've got an e-mail of DIA dot dotcom and they've already responded.
So no this is not a record I care about yet.
I didn't look at the next recipient side the list.
And I notice that it has an e-mail of a EYKAMP and it has responded false.
And so clearly this recipient right here matches my criteria.
I had wanted to find someone with an email of a dot.com who had not yet responded to the survey.
And so after correlating or kind of checking the id property the email property and the respondent property
I would then say OK clearly this is in fact the survey that I want to find and the one that I want to
update now as I start to as i'm like saying this stuff you might be sitting there thinking well wait
a minute what's this about the recipient.
Why can't we just assume that hey you got a survey idea four five six.
Clearly this is the one you care about right here.
Well what I'm saying is that if we can write some type of query that attempts to find a record with
the id the given e-mail and respondent property are false then we can write a query that lets Mongo
execute all of this search logic for us and you and I should never actually have to take the survey
and pull it back into the No.
Yes world.
In other words we can write a query that is executed entirely inside of the Mongo database and never
once requires us to actually load any data back over to the Express side of our application.
And so that would solve all the issues that I was just talking about with this other kind of junky query
over here where we were loading up not only a survey but the entire list of recipients.
So we are pulling all this data from Mongo over to the Express side of the world.
So what I'm suggesting here is that if we consider not only the ID property in our search criteria but
also the email and responded properties then we can write a single query that will always return exactly
just the data that we really truly care about.
Now I want to give you a quick example here what would happen if I put responded of true in here instead.
So we said that OK this person has the e-mail but they have already responded to the survey.
So now if we start to think about the same exact query that we were just talking about let's kind of
walk through it on this record right here.
We would say OK it's got the correct survey ID 4 5 6 6.
Now does it have an email of a at a dot com for one of the recipients would say OK that's DRDO dot com
we don't care about that one.
Oh here it is at a dot com.
So it's a yep that fits our criteria.
And now the very last piece of criteria does that same record have a responded property of false.
Well no it doesn't anymore now it has responded.
Property of true.
And so we would say oh this survey right here it does not actually match our search criteria.
This must not be a survey that we are actually truly carrying or that we actually truly care about.
So let's go and look at the next survey and see if maybe that one matches.
So clearly if we just look at all three properties for figure out which survey we need to update we
can kind of include or we can kind of disqualify any surveys that don't have the correct three properties.
Now at this point I'm kind of talking myself into a circle here.
As you can kind of tell I as a quick Editor's note here.
I want to tell you that whenever you're figuring out these really complex Mongo queries life is kind
of tough and I'm trying to give you a little bit of a walkthrough of giving you just like at least a
hint of an idea of how you kind of approach writing these queries after we actually write the code around
this.
I'm gonna show you a method that you can use to kind of arrive at this logic of how we write all these
queries out much more easily than you know.
I'm kind of trying to describe it to you and the words I can give you a little bit of a toolbox or a
tool set for putting queries together that do all this update logic efficiently.
OK.
So I think I've kind of you know again spoken myself into a corner here with all this update logic stuff.
I want to say let's just flip over to our code editor let's try to write out some type of query that
will take a given event like this thing right here and then tried to find the appropriate survey to
update.
So we flip on over to my code editor and I don't really care about writing this code inside of our route
handler just yet because I feel like that would kind of confuse things.
So I'm going to open up a new file.
I'm not going to say this I just want to use it as a possible example.
Oops I definitely hit the wrong button there.
And we're going to write our sample query right into here just kind of in total isolation so we can
kind of get a better sense of what's going on.
So we're going to say look at our collection of surveys and find one record that matches this given
criteria that we are about to provide.
So the first piece of criteria that we really care about here is making sure that we find a survey that
has an ID of survey ID.
Right that makes sense.
We want to make sure that we have an ID with our survey with the correct ID.
Now the next piece of query logic in here the next kind of filter that we're going to provide to this
query is to make sure that we are also finding a survey that has a recipient with the correct email
property.
And they responded property of false because that's what we really want.
So this next piece of logic right here this next kind of filtering criteria or this next search criteria
that we're going to pass in is going to be a little bit more complex.
We're going to say find a survey that not only has the correct survey ID but also look into its recipients
sub document collection inside of that sub document collection find an element that matches some given
criteria.
So we're going to say dollar sign L-M match and then we're going to pass it the kind of sub criteria
that we want to do as a search here and we'll talk about this Elham match thing is in just a second.
So we're going to say one of the recipients inside the recipient of document collection should have
an email of email and responded property of false like so.
So this is pretty much the actual query that we're going to end up writing right here.
Let's walk through the entire thing.
We say go through the entire survey collection and find exactly one record that has an I.D. of survey
I.D..
So hopefully this first part right here this is you know pretty straightforward.
Not too bad.
The second part right here is where things get really interesting.
So this little block of code right here says for every survey in that service collection look through
its recipient's property the recipient's property should be a sub document collection that has an array
of records.
So go through all those different elements go through all the different sub documents and find one element
that matches this given criteria right here specifically a recipient who has an e-mail of e-mail and
you know presumably we're going to define the e-mail ahead of time appear like a dot com or something
like that.
And then also make sure that that recipient has a responded property of false.
So this query right here would pretty much execute what we just walk through over here.
We would say make sure that it has the correct I.D. make sure it has the correct e-mail and make sure
it has the respondent property are false.
And if the survey doesn't have all three of those criteria true.
Well then we must not have a record that fits the bill here.
Go and find some other record and discontinue searching through the collection until you find one that
satisfies all these requirements.
And of course you and I know that we don't have any other record that satisfies those requirements.
OK.
So I think that maybe you've got a reasonable sense of what this court is going to do at this point.
So we're saying Go and find one record that matches all of the search criteria.
Now we're going to throw kind of an extra level in here at this point we've only been talking about
kind of finding this one record and then presumably we would be turning it back over to our express
server updating it and then send you back on to.
But I would suggest maybe we can not only find the exactly correct survey with the exactly correct recipient
but we can also update that same record at the exact same time that is if it is found inside of our
survey collection.
So rather than saying find one record inside of our surveys collection we're going to use a different
method that is included on our mongoose model class called update 1.
So this update one function right here says Go and find a record with this given search criteria.
After that record is found.
Take the second object that we are going to provide as an argument to this thing and update the record
that was just found with this object right here.
And so that's really what we want to do right.
And one big go.
We want to find the correct survey and then immediately update it.
And we want this update to be entirely done inside of the Mongo world.
We do not really want to pull this survey over to our express server at all.
We don't care about this survey or inside of our handler right now.
Right.
If we go back over a survey routes we don't care about getting a handle on that survey.
We just care about making sure that we update the survey and nothing else.
So if we add in the second argument here and passing an object that describes how we want to update
the record the entire update will be taken care of inside of the Mongo database.
And we never have to actually load any of this data back over to express.
So at this point I think we've covered a pretty good number of topics.
Let's take a quick break and then we'll continue in the next section and talk about exactly what data
we're going to put inside the second object to describe how that survey should be updated.
So let's take a quick break and I'll see you in just a minute.