I everyone will come back.
So in this video it's going to be the last video that we will be discussing stuff on Mullany.
So should be the last video.
So yeah let's get started.
So updating documents in mongerer Bebe's so Mungo maybe has two functions update and see Mathes and
they're used to update the documents that are already in a college.
So if you already answered that a document into a collection then you can update them were changed and
using the update or the same function.
Now the update method updates the values in the existing document while the Save method replaces the
existing document with the document passed in the Save method.
So what does that mean.
Will the update function basically looks for the records that you want to or that you want to update
and that it doesn't in-place update.
So for example likes where 50 then it would just change this 50 to 100.
For example if you want to change you to a hundred.
But for the save function what it does is it just scraps the whole document creates a new document that
is exactly identical to this.
But the only difference is likes is our 100.
OK.
So that's the only difference between the two.
Now the update method updates the values in the existing document and you can use it by doing D-B collection
in that update.
And then the selection criteria and the updated data.
OK.
So we're going to talk about that and show you some examples.
OK.
So consider them my collection collection which has the following data.
So we have two objects here and then we have one title.
Mongo D-B overview with an ID here and another one with an ID and a title.
OK No.6 overview.
OK.
So basically following example will set the new title new Mongo D-B tutorial of the documents whose
title is Maunganui Baeza.
We're going to replace this title lung would be overview with new Mongo NDB tutorial.
OK.
So we're going to do a DVD on Michael update update and then I'm going to specify the criteria that
I want to that I want to change and or that I want to search for.
In this case I want to search for all the documents that have a title.
Mangu NDB overview and then a comma.
And then what I want to says if you're a member here there's two parts right.
There is a selection criteria so my selection criteria here is that I want the title to have a mango
D-B overview and then in the updated data over here the new data that I want and when I set dollar said
so very important that I'm not to set the title to mean new Mongo IDB tutorial in the dark and then
close the curly brackets.
Now if I show all the documents I'm going to do a DBD on my column data find.
So I'm going to show a scene that an ID is now this same idea over here.
So if you see here this is the same as this ID over here.
But then the title change from Mangu honeybee or wherever you are to new Mago D-B overview.
OK.
And also this one remain the same right because it wasn't inside this selection criteria.
Now by default Mungo will update only a single document.
OK so just make sure you understand the only update a single dog can.
So if you want to update multiple documents you need to set up parameter Multi to true okay.
And the way you do it is and this is a very useful tip is OK how your selection criteria over here.
This is your selection criteria.
And then a comma right.
I hid it from you so there's a comma and then you have a set.
But then before the set if you notice you're used when open the curly brackets right so you have said
over here and then in the end.
After that after you specify you're set right there is a comma another comma.
And then you have another curly brackets with multi Ignaz true.
OK.
So multi as true means that what you're doing essentially is you're saying any document which satis
which has this title mongered me over.
You change all of its titles to new monk would you be DOYLE So not just a single document but all the
documents in in the Mongo D-B.
Now what if we want to delete the dog.
So we thought about updating documents and inserting documents.
What about deleting documents well mongerer need be removed so damali TV has a remove function or method
that is used to remove a document from the collection.
Now remove method access to parameters.
One is the deletion criteria and the second is just one flag.
So what are those.
So the deletion criteria basically says which.
What type of criteria or what kind of conditions must a document have such that it's deleted.
Right.
So for example if I have if I if I inserted the the the the deletion criteria as likes and then 100
then I'm going to delete the documents which have 100 likes.
So they have key as likes and 100 as a value.
Now the just one is an optional parameter.
And if it's true that removes only one document.
But if it's false then it removes all the documents which have 100 likes.
So if just one is true that any post or any document which has 100 likes is deleted from the collection.
But if it's only just one that one of them has only delete.
Now the basic syntax for move is to do D-B collection name don't remove and then deletion criteria.
And if you want to insert the just one that's also optional.
So let's take an example.
So let's say you have a collection with the following data.
You have an ID and an object id and then you also have a title and a and the type corresponding title
and then so Mongo D-B overview and then you have another ID object with ID and a title as Nosenko overview.
Now the following example will remove all the documents from that whose title is Mungo D-B overview.
So you do DBD.
My collections are removed.
Title Mangione overview.
So it searches for all the documents which have a title as Mangione beats this one over here.
Mago Nimi overview and it deletes it so this object here is going to be deleted.
Now if I after I remove them I do a find just to search for all of all the documents I would find only
one document which is the second one because the first one is deleted.
So hopefully this makes sense.
OK so now we're going to talk about projection.
What is projection.
Well a mongo to be a projection means selecting only the necessary data rather than selecting the whole
data of the document.
A document has five fields and you need to only show three that select only three fields from that.
So what does that mean.
So let's suppose you're doing a fine for you.
OK.
For some criteria right now suppose your document is very very big so you suppose your documents you
do right.
This is huge right now and it has a ton of fields ready and its size is big.
Now suppose you only want to have the fields in this huge document and you don't need the rest.
So what projection does is you tell it hey after you find it don't return to me.
Everything in this document right.
Don't return the whole document.
Just return the field that I'm interested about for example the first field in this I could feel that
way.
Mongo doesn't have to copy the entire collection into you know enjoying array to give you but instead
just takes one or two fields that you really need and returns them to you.
Now the Navy's fine method is explained before so it accepts a second optional parameter.
And that is a list of fields that you want to retrieve.
So in Lango AB When you execute fine method it will display all the fields off a document write or display
the whole block and write unified as we saw previously here.
So if I fly quickly show you in this example this displays the entire document but it what if I only
need to see the title and the description what if I don't want the ID tags or likes.
So how do I do that.
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
while 0 is used to hide the field.
So one shows that tells Mongo and I want that field and zero shows that you don't want that field.
So let's take an example.
So the basic syntax a find method with projection is as follows.
So you knew D-B the collection name.
So my collection for example and then not fine.
And then this is the selection criteria that we always select and then come up and then you give it
another document with key 1.
So you say I want that key give it give it back to me.
So consider the collection my column that has the following data.
So we have again the same collection that has to do documents one with ID title and another with ID
and title and each of them are different.
Right.
Because they have different ideas and they have different titles.
Now the following example would display the title of the document.
While querying the document.
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
that find and then this empty document means I'm looking for all the documents right.
There's no selection criteria so any document passes this right so that empty one means just bring everything
in the collection and then but I only want the title so I'll set the title as one and the ID is yours.
I don't want the ID don't return to me the ID just returned the title as the result of this would be
this.
Over here this is their result will be two documents was the title as Mongo D-B overview and title is
no secret.
So if you notice here there is no ID.
So in other words non-good you just returned to me the fields that I was interested in title and not
ID.
Now there's also a case of limiting records.
Let's say you have a record with 100000 customers.
Right so you don't want long Goody-Goody.
Let's say you do find and then your criteria is in general and you just want to a few examples of records
you don't want.
For example all the 100000 or all like 20000 You just want like 100 or something just to look at the
data.
So to limit the records in longer D-B you need to use limit method.
So the method accepts one number type argument which is the number of documents that you want to be
displayed so the maximum number that you see that you want to see.
So D-B duck collection they find and then to limit number.
So that means that even if this returns you know 300000 records right.
This area over here abridgments three others.
Alan if this is just five then you only see five.
So this prevents it from having you know a gigantic returning gigantic list of documents right.
So let's take an example.
So consider the fall the collection my collection that has the following data.
So again we have one and two we have these two objects the same ones.
And so the following example will display only one document while querying the document.
So Ill do D-B that my collection does fine and then empty criteria so all the documents and I want to
see the title and not the ID.
And I also limit that to one.
So I don't want all the documents I just want to limit that to one.
So it returns to me only one documents even though there's two documents that satisfy this criteria
so I'm limiting the number of returns to only one documents instead of two.
So if you don't specify the number argument in limit method then it will display all the documents from
that collection.
So if you just do a limit and a do like this then it's pointless.
This is like if you not call it it will just display all of the documents from the collection that satisfy
the selection criteria.
Now another thing that you might want to do is to sort some documents so to say in documents in language
you need to use the search function.
So the sort of method this method accepts a document containing a list of fields along with their sorting
order to specify sorting order one or negative one or use.
So one is for ascending order and negative one is for descending order and this is the basic syntax
you do D-B collection that find and then you show it.
So it does sort and then you sort that based on this key over here and then the one means sorted in
ascending order based on this key over here.
So an example might be D-B DOD.
My collection does find.
Right.
And then what sort and then you'd open curly parentheses and this this is find.
Right.
And then this is curly brackets here.
And then you would say likes and then one.
So that means to sort them in ascending order based on the number of likes.
So hopefully that makes sense.
So yeah that's it for Mongo D-B we're going to stop here.
And that will be the last lesson in explaining how Mongo DBI works.
The next set of lessons is basically going to be heading straight into it.
And you know programming something with it just to get used to the syntax and not just cover a you know
theory of the stuff to see how this is really used and how we can use it in building our API.
So yeah we'll stop here and I'll see you in the next video.
So until the next video app decoding.