In this video we're going to get a better idea of how Mongoose and Mongo DB collaborate to set up and
use this version field stuff.
All right.
First you're going to do is take a look at how it Mongoose and Mongo DB normally collaborate to handle
record updates.
So this is it without this version flag or anything like that.
So inside of our ticket service we might have some code that's going to fetch record season.
Q We might then run some update on a document.
So for example set its price to 10 or 15 or whatever else.
Well then tell Mongoose to save the document and very critically when that occurs mongoose is going
to reach out to Mongo DB and it's going to tell Mongo D.B. hey I want you to do an update on record
season.
Q Does going to send off some kind of request to Mongo DB.
And a very critical part here is that inside this request there's something that says bind the record
with I.T. C.C. Q and satisfies you 10 so Mongo DB internally is going to look at its collection of tickets.
It's gonna find a record with IDC Q And then update its price to 10 and that's it.
That's the entire process.
Now the thing I really want to emphasize here is that in the request to update this record there is
something that says find specifically at the record with Idi Caesar.
Q Okay so if all that mind let's not take a look at the same flow but with all this version tracking
stuff enabled we're going to see how Mongoose and Margaux D we collaborate to make sure that we only
update records with the appropriate version number OK so same kind of flow here.
But now we're imagining that this version Fielding is being incorporated or included and just you know
this entire process that we're going through is referred to as optimistic concurrency control.
And if you want to you could do a quick google search take a look at a blog post or two and read a little
bit more about it.
The entire strategy that we're talking about here for optimistic concurrency control at Mongo DV is
100 percent applicable to other types of databases as well.
So what we're discussing here is not isolated to mongo DV at this is not a mango DB specific strategy
you can use the strategy with many other types of databases very easily OK.
So in this flow we're going to once again fetch a record from Susie Q run update on the documents save
the document and then internally we're going to turn a setting on inside a mongoose that's going to
cause Mongoose to automatically update the version field of the document automatically.
So that would be where we increment the version field from say one to two to three three to four or
whatever else mongoose is then once again going to send an update request off to Mongo DB.
And now now that we are implementing this versioning stuff here is the critical critical difference
inside this request mongoose is going to tell Mongo DV to find the record with IDC Q and A version of
one and set its price to 10.
That is the critical little step.
So whenever Mongo DB it tries to update to record it's not actually going to try to compare these version
numbers and say hey is this less than 1 or is this less than the current first number or whatever else
that doesn't actually occur.
What really happens is mongoose is going to say find the record with this particular I.D. And this particular
version.
So it's in how we find this record that's going to make sure that we select some record with the correct
version.
Now let's imagine the scenario in which we do have a record inside of our database with an idea a Q
and A version of one.
We would find this record right here.
It has the correct I.D. and the correct version and we had set its price to 10 and that's it.
Everyone is happy now.
What would happen if something goes not quite right.
Well I'm going to roll this back.
Now let's imagine that we accidently process the 15 dollar price update first.
So that would have been the event that we discussed just a moment ago.
This one right here where we're trying to set the price to 15 and we would be looking for a record inside
of our database with a version of two because this thing has a version of three so in that scenario
this query right here would instead say find the record with DCC Q and A version of two and set its
price to 15.
Now as you can guess Mongo DB is gonna run a query over the ticket's collection.
It's gonna look for a record with a idea of season Q and A version of two so we can look through all
these records and there's no record that matches that query.
There's no record that an idea sees a Q and A version of two.
There is one with an idea Cizik universe of one.
But that's not what we're asking for we're asking for one with a version specifically of two.
And so this request right here this update would fail and this error would be returned to our application
where we would be forced to deal with it in some fashion but that's it.
That is what is going on behind the scenes.
It's really all hinging upon the fact that when we send over these requests to update record we're including
not only the idea of the record we're trying to update but also the version of the record we're trying
to update as well.
And that is what is used for the criteria to find the record that we're trying to make a change to not
thing you'll notice here is that this is all about updating records.
It's not about inserting records.
So the assumption is that when we first insert a record it's always going to default to a version of
0 or 1.
So we don't really run into a big issues with inserting records.
Not really a big concurrency issue there.
It's a bigger issue when we're trying to update existing records.
That's when we start to get into big trouble.
And that's where this version flag is going to start to become really really handy.
So that's it.
That's the idea.
So as you guess Well we have to make a little change or two to mongoose.
We'll take a quick pause right here in the next video we're going to see how to actually implement this
stuff in the world a mongoose.
We're also going to write out a test or two inside replication so you can actually write some code and
immediately start to work with this version stuff and get a better idea of what's going on with it.