WEBVTT

00:05.090 --> 00:06.860
I everyone will come back.

00:06.870 --> 00:13.320
So in this video it's going to be the last video that we will be discussing stuff on Mullany.

00:13.440 --> 00:15.600
So should be the last video.

00:15.610 --> 00:17.500
So yeah let's get started.

00:17.520 --> 00:23.490
So updating documents in mongerer Bebe's so Mungo maybe has two functions update and see Mathes and

00:23.490 --> 00:27.150
they're used to update the documents that are already in a college.

00:27.150 --> 00:32.460
So if you already answered that a document into a collection then you can update them were changed and

00:32.460 --> 00:34.940
using the update or the same function.

00:34.950 --> 00:40.860
Now the update method updates the values in the existing document while the Save method replaces the

00:40.860 --> 00:44.370
existing document with the document passed in the Save method.

00:44.370 --> 00:45.180
So what does that mean.

00:45.180 --> 00:52.730
Will the update function basically looks for the records that you want to or that you want to update

00:53.120 --> 00:55.310
and that it doesn't in-place update.

00:55.310 --> 01:00.970
So for example likes where 50 then it would just change this 50 to 100.

01:00.970 --> 01:03.240
For example if you want to change you to a hundred.

01:03.710 --> 01:09.320
But for the save function what it does is it just scraps the whole document creates a new document that

01:09.320 --> 01:11.230
is exactly identical to this.

01:11.390 --> 01:15.330
But the only difference is likes is our 100.

01:15.370 --> 01:15.930
OK.

01:16.100 --> 01:18.550
So that's the only difference between the two.

01:18.560 --> 01:26.230
Now the update method updates the values in the existing document and you can use it by doing D-B collection

01:26.230 --> 01:28.200
in that update.

01:28.280 --> 01:31.720
And then the selection criteria and the updated data.

01:31.720 --> 01:32.110
OK.

01:32.120 --> 01:35.780
So we're going to talk about that and show you some examples.

01:35.780 --> 01:36.040
OK.

01:36.050 --> 01:40.870
So consider them my collection collection which has the following data.

01:40.880 --> 01:44.280
So we have two objects here and then we have one title.

01:44.300 --> 01:49.430
Mongo D-B overview with an ID here and another one with an ID and a title.

01:49.440 --> 01:51.010
OK No.6 overview.

01:51.120 --> 01:51.690
OK.

01:51.800 --> 01:58.940
So basically following example will set the new title new Mongo D-B tutorial of the documents whose

01:58.940 --> 02:00.260
title is Maunganui Baeza.

02:00.260 --> 02:05.160
We're going to replace this title lung would be overview with new Mongo NDB tutorial.

02:05.200 --> 02:05.800
OK.

02:06.140 --> 02:12.080
So we're going to do a DVD on Michael update update and then I'm going to specify the criteria that

02:12.080 --> 02:15.860
I want to that I want to change and or that I want to search for.

02:15.860 --> 02:19.700
In this case I want to search for all the documents that have a title.

02:19.790 --> 02:23.410
Mangu NDB overview and then a comma.

02:23.480 --> 02:26.580
And then what I want to says if you're a member here there's two parts right.

02:26.580 --> 02:31.910
There is a selection criteria so my selection criteria here is that I want the title to have a mango

02:31.910 --> 02:39.060
D-B overview and then in the updated data over here the new data that I want and when I set dollar said

02:39.160 --> 02:45.500
so very important that I'm not to set the title to mean new Mongo IDB tutorial in the dark and then

02:45.500 --> 02:47.590
close the curly brackets.

02:47.600 --> 02:52.690
Now if I show all the documents I'm going to do a DBD on my column data find.

02:52.700 --> 02:57.810
So I'm going to show a scene that an ID is now this same idea over here.

02:58.010 --> 03:01.100
So if you see here this is the same as this ID over here.

03:02.550 --> 03:07.280
But then the title change from Mangu honeybee or wherever you are to new Mago D-B overview.

03:07.440 --> 03:07.970
OK.

03:08.220 --> 03:15.320
And also this one remain the same right because it wasn't inside this selection criteria.

03:15.660 --> 03:19.470
Now by default Mungo will update only a single document.

03:19.470 --> 03:24.170
OK so just make sure you understand the only update a single dog can.

03:24.270 --> 03:30.090
So if you want to update multiple documents you need to set up parameter Multi to true okay.

03:30.150 --> 03:36.710
And the way you do it is and this is a very useful tip is OK how your selection criteria over here.

03:36.750 --> 03:38.640
This is your selection criteria.

03:38.880 --> 03:40.200
And then a comma right.

03:40.290 --> 03:43.960
I hid it from you so there's a comma and then you have a set.

03:44.160 --> 03:49.090
But then before the set if you notice you're used when open the curly brackets right so you have said

03:49.110 --> 03:52.240
over here and then in the end.

03:52.250 --> 03:56.870
After that after you specify you're set right there is a comma another comma.

03:57.000 --> 04:00.040
And then you have another curly brackets with multi Ignaz true.

04:00.120 --> 04:00.600
OK.

04:00.720 --> 04:07.350
So multi as true means that what you're doing essentially is you're saying any document which satis

04:07.410 --> 04:10.080
which has this title mongered me over.

04:10.090 --> 04:17.100
You change all of its titles to new monk would you be DOYLE So not just a single document but all the

04:17.100 --> 04:21.030
documents in in the Mongo D-B.

04:21.030 --> 04:23.100
Now what if we want to delete the dog.

04:23.220 --> 04:27.310
So we thought about updating documents and inserting documents.

04:27.330 --> 04:34.110
What about deleting documents well mongerer need be removed so damali TV has a remove function or method

04:34.110 --> 04:37.110
that is used to remove a document from the collection.

04:37.110 --> 04:39.720
Now remove method access to parameters.

04:39.840 --> 04:45.250
One is the deletion criteria and the second is just one flag.

04:45.420 --> 04:46.230
So what are those.

04:46.260 --> 04:49.680
So the deletion criteria basically says which.

04:49.680 --> 04:57.480
What type of criteria or what kind of conditions must a document have such that it's deleted.

04:57.480 --> 04:57.880
Right.

04:57.960 --> 05:06.150
So for example if I have if I if I inserted the the the the deletion criteria as likes and then 100

05:06.360 --> 05:10.690
then I'm going to delete the documents which have 100 likes.

05:10.710 --> 05:15.040
So they have key as likes and 100 as a value.

05:15.060 --> 05:17.510
Now the just one is an optional parameter.

05:17.520 --> 05:20.350
And if it's true that removes only one document.

05:20.550 --> 05:24.740
But if it's false then it removes all the documents which have 100 likes.

05:24.780 --> 05:33.370
So if just one is true that any post or any document which has 100 likes is deleted from the collection.

05:33.540 --> 05:37.230
But if it's only just one that one of them has only delete.

05:37.500 --> 05:45.720
Now the basic syntax for move is to do D-B collection name don't remove and then deletion criteria.

05:45.720 --> 05:50.670
And if you want to insert the just one that's also optional.

05:50.700 --> 05:52.280
So let's take an example.

05:52.530 --> 05:57.290
So let's say you have a collection with the following data.

05:57.450 --> 06:06.000
You have an ID and an object id and then you also have a title and a and the type corresponding title

06:06.360 --> 06:14.840
and then so Mongo D-B overview and then you have another ID object with ID and a title as Nosenko overview.

06:15.330 --> 06:21.840
Now the following example will remove all the documents from that whose title is Mungo D-B overview.

06:22.840 --> 06:24.250
So you do DBD.

06:24.340 --> 06:26.210
My collections are removed.

06:26.290 --> 06:28.610
Title Mangione overview.

06:28.750 --> 06:33.910
So it searches for all the documents which have a title as Mangione beats this one over here.

06:33.910 --> 06:39.970
Mago Nimi overview and it deletes it so this object here is going to be deleted.

06:39.970 --> 06:46.960
Now if I after I remove them I do a find just to search for all of all the documents I would find only

06:46.960 --> 06:51.680
one document which is the second one because the first one is deleted.

06:51.730 --> 06:53.330
So hopefully this makes sense.

06:55.800 --> 06:57.910
OK so now we're going to talk about projection.

06:57.920 --> 06:59.130
What is projection.

06:59.300 --> 07:05.000
Well a mongo to be a projection means selecting only the necessary data rather than selecting the whole

07:05.000 --> 07:06.730
data of the document.

07:06.800 --> 07:11.820
A document has five fields and you need to only show three that select only three fields from that.

07:11.960 --> 07:12.770
So what does that mean.

07:12.770 --> 07:15.090
So let's suppose you're doing a fine for you.

07:15.130 --> 07:16.250
OK.

07:16.340 --> 07:23.320
For some criteria right now suppose your document is very very big so you suppose your documents you

07:23.340 --> 07:23.960
do right.

07:23.960 --> 07:29.450
This is huge right now and it has a ton of fields ready and its size is big.

07:29.870 --> 07:36.380
Now suppose you only want to have the fields in this huge document and you don't need the rest.

07:36.620 --> 07:41.670
So what projection does is you tell it hey after you find it don't return to me.

07:41.690 --> 07:43.420
Everything in this document right.

07:43.420 --> 07:45.510
Don't return the whole document.

07:45.590 --> 07:51.110
Just return the field that I'm interested about for example the first field in this I could feel that

07:51.110 --> 07:51.480
way.

07:51.480 --> 07:58.130
Mongo doesn't have to copy the entire collection into you know enjoying array to give you but instead

07:58.130 --> 08:03.710
just takes one or two fields that you really need and returns them to you.

08:03.800 --> 08:10.260
Now the Navy's fine method is explained before so it accepts a second optional parameter.

08:10.430 --> 08:13.490
And that is a list of fields that you want to retrieve.

08:13.490 --> 08:19.430
So in Lango AB When you execute fine method it will display all the fields off a document write or display

08:19.430 --> 08:23.900
the whole block and write unified as we saw previously here.

08:23.900 --> 08:30.890
So if I fly quickly show you in this example this displays the entire document but it what if I only

08:30.890 --> 08:35.740
need to see the title and the description what if I don't want the ID tags or likes.

08:35.930 --> 08:37.470
So how do I do that.

08:38.120 --> 08:45.200
So to limit this you need to set a list of fields with value 1 or 0 and this is used to show the fields

08:45.310 --> 08:47.110
while 0 is used to hide the field.

08:47.110 --> 08:54.230
So one shows that tells Mongo and I want that field and zero shows that you don't want that field.

08:54.260 --> 08:55.670
So let's take an example.

08:58.360 --> 09:02.280
So the basic syntax a find method with projection is as follows.

09:02.390 --> 09:04.760
So you knew D-B the collection name.

09:04.760 --> 09:08.020
So my collection for example and then not fine.

09:08.210 --> 09:13.880
And then this is the selection criteria that we always select and then come up and then you give it

09:13.880 --> 09:16.390
another document with key 1.

09:16.520 --> 09:20.090
So you say I want that key give it give it back to me.

09:20.090 --> 09:23.240
So consider the collection my column that has the following data.

09:23.240 --> 09:31.350
So we have again the same collection that has to do documents one with ID title and another with ID

09:31.370 --> 09:33.530
and title and each of them are different.

09:33.540 --> 09:33.980
Right.

09:34.220 --> 09:37.550
Because they have different ideas and they have different titles.

09:37.610 --> 09:41.090
Now the following example would display the title of the document.

09:41.180 --> 09:42.920
While querying the document.

09:42.920 --> 09:49.730
So let's say we don't care about the idea we just want to return the title so I can divvied up my column

09:49.730 --> 09:54.110
that find and then this empty document means I'm looking for all the documents right.

09:54.110 --> 10:00.920
There's no selection criteria so any document passes this right so that empty one means just bring everything

10:00.980 --> 10:07.820
in the collection and then but I only want the title so I'll set the title as one and the ID is yours.

10:07.820 --> 10:14.710
I don't want the ID don't return to me the ID just returned the title as the result of this would be

10:14.710 --> 10:14.950
this.

10:15.050 --> 10:22.730
Over here this is their result will be two documents was the title as Mongo D-B overview and title is

10:22.730 --> 10:23.330
no secret.

10:23.330 --> 10:25.440
So if you notice here there is no ID.

10:25.730 --> 10:30.920
So in other words non-good you just returned to me the fields that I was interested in title and not

10:30.920 --> 10:34.020
ID.

10:34.030 --> 10:36.670
Now there's also a case of limiting records.

10:36.660 --> 10:40.170
Let's say you have a record with 100000 customers.

10:40.200 --> 10:41.840
Right so you don't want long Goody-Goody.

10:41.840 --> 10:49.720
Let's say you do find and then your criteria is in general and you just want to a few examples of records

10:49.790 --> 10:50.360
you don't want.

10:50.360 --> 10:56.810
For example all the 100000 or all like 20000 You just want like 100 or something just to look at the

10:56.810 --> 10:57.410
data.

10:57.590 --> 11:01.650
So to limit the records in longer D-B you need to use limit method.

11:01.760 --> 11:06.980
So the method accepts one number type argument which is the number of documents that you want to be

11:06.980 --> 11:10.310
displayed so the maximum number that you see that you want to see.

11:10.450 --> 11:15.040
So D-B duck collection they find and then to limit number.

11:15.050 --> 11:20.700
So that means that even if this returns you know 300000 records right.

11:20.720 --> 11:22.780
This area over here abridgments three others.

11:22.820 --> 11:26.210
Alan if this is just five then you only see five.

11:26.300 --> 11:34.720
So this prevents it from having you know a gigantic returning gigantic list of documents right.

11:34.910 --> 11:36.220
So let's take an example.

11:36.230 --> 11:39.980
So consider the fall the collection my collection that has the following data.

11:39.980 --> 11:44.250
So again we have one and two we have these two objects the same ones.

11:44.540 --> 11:49.630
And so the following example will display only one document while querying the document.

11:49.790 --> 11:56.840
So Ill do D-B that my collection does fine and then empty criteria so all the documents and I want to

11:56.840 --> 11:59.710
see the title and not the ID.

12:00.050 --> 12:02.360
And I also limit that to one.

12:02.390 --> 12:05.860
So I don't want all the documents I just want to limit that to one.

12:06.050 --> 12:12.320
So it returns to me only one documents even though there's two documents that satisfy this criteria

12:12.770 --> 12:17.730
so I'm limiting the number of returns to only one documents instead of two.

12:17.750 --> 12:23.810
So if you don't specify the number argument in limit method then it will display all the documents from

12:23.810 --> 12:24.630
that collection.

12:24.740 --> 12:29.450
So if you just do a limit and a do like this then it's pointless.

12:29.450 --> 12:35.420
This is like if you not call it it will just display all of the documents from the collection that satisfy

12:35.720 --> 12:37.200
the selection criteria.

12:39.220 --> 12:44.140
Now another thing that you might want to do is to sort some documents so to say in documents in language

12:44.500 --> 12:46.630
you need to use the search function.

12:46.630 --> 12:52.510
So the sort of method this method accepts a document containing a list of fields along with their sorting

12:52.510 --> 12:56.390
order to specify sorting order one or negative one or use.

12:56.410 --> 13:02.480
So one is for ascending order and negative one is for descending order and this is the basic syntax

13:02.560 --> 13:06.970
you do D-B collection that find and then you show it.

13:06.990 --> 13:15.400
So it does sort and then you sort that based on this key over here and then the one means sorted in

13:15.400 --> 13:18.980
ascending order based on this key over here.

13:18.990 --> 13:22.590
So an example might be D-B DOD.

13:22.710 --> 13:25.920
My collection does find.

13:25.950 --> 13:26.490
Right.

13:26.770 --> 13:33.860
And then what sort and then you'd open curly parentheses and this this is find.

13:33.880 --> 13:34.450
Right.

13:35.310 --> 13:38.200
And then this is curly brackets here.

13:38.370 --> 13:43.160
And then you would say likes and then one.

13:43.190 --> 13:48.190
So that means to sort them in ascending order based on the number of likes.

13:48.190 --> 13:50.230
So hopefully that makes sense.

13:51.050 --> 13:54.400
So yeah that's it for Mongo D-B we're going to stop here.

13:54.420 --> 13:58.650
And that will be the last lesson in explaining how Mongo DBI works.

13:58.650 --> 14:02.980
The next set of lessons is basically going to be heading straight into it.

14:02.990 --> 14:09.110
And you know programming something with it just to get used to the syntax and not just cover a you know

14:09.130 --> 14:15.270
theory of the stuff to see how this is really used and how we can use it in building our API.

14:15.600 --> 14:18.470
So yeah we'll stop here and I'll see you in the next video.

14:18.480 --> 14:20.640
So until the next video app decoding.
