[MUSIC] Hello and welcome back. In this lecture we will look at some basic CRUD operations. Create, find, update, upsert, and delete. The first operation we look at is create. Now create does two things. One, it instantiates, or creates, a transient object and then it does a save. So create will do two things. One, a transient object, new. And then a save which will persist the data in the database. The other important thing is if the collection does not exist, create will automatically create a collection, and then, insert the data into the database, so let's go and give this example a try. So let me paste the movie.create, so there it is, so our movie just got created and the data is persisted into the database. Now, how do we know it's saved in the database? Let's do a find and see if we get the data back. So I'll do a find by title Martian. So there is the ID 008 and there is 008, we get the data back. Now the next operation is a save. Now save is two part process first you do a new, which is creating a transient object, or instantiating a transient object, and then save will actually save the data. So in this example, we do a movie, Rocky. And then, if you do a find before you do a save, you will not get the document back. And then you do a save, you'll get the data. Let's go and try this first. So let me just paste that example. So there is the movie.new, title Rocky. So this actually created or just a transient object, you can do a lot of things in movie, but it's not persisted into the database, right. You have accessed the Movie object, but it's not in the database. Now we can test this by doing a simple find, and we should get an error. Say if I tried to a Movie find by title Rocky, it'd say it's Document not found for class Movie with attributes title Rocky, right? So we have to actually do a Movie.save. So, I have to explicitly do a movie.save, and now if I do a command recall and try to find my movie, I should get my movie with ID 009. Now I can do a quick update by doing movie.year, pick one of the attributes like year, and do a change, and then do a save. It will update the document. So that's at movie.year in 1986, and I'll do the movie.save. So do a movie.save, right? Now if I go back and do my find by, I should see that year set to 1986, so rest of the movie information is still the same. The movie is Rocky, rated R, year is set to 1986 okay. You can also do an update using update attributes. So in this example this is our movie object. You can go back and update the document rated from PG-13 to R. Let's try this example. So copy and paste. So there is my movie. The movie.new So I'll do a movie.save, save the document into the database, let's do a simple find, so there is our document right? So now, let's go and do an update attributes to make the update. So remember the rating for this movie was PG 13, and we want to change the movie.update attributes rated to R. Okay. Now let's first go and see what do we have for Rocky 31. Notice how it used to be PG13, and now the rating has changed to R. Okay. Next, let's take a look at upsert. Now, upsert the keyword upsert will actually you know, perform a MongoDB quote and quote upsert on the document. It basically means that if a document exists, it will overwrite the document, but if the document does not, it will actually create a new one and insert the data. So let's see an example. Probably the best way to understand this is with an example. So let's go and insert a movie, so we'll use the same Rocky31. So we have ID 005. Let's go and do a quick find to make sure it's there. Okay, 005. So now let's do an upsert. So what I'll do is I'll take the same movie, right? And I will take the ID 005 and title Rocky31. But I want to change the rating from PG13 to R. So what will happen is, this command will actually go and check to see if this ID exist. If it does, then it will simply do an update, and change the rating from PG13 to an R, so lets try that. Okay it looks like that was successful. Just to make sure everything is good, I'll do a quick find by, and there is the same ID, and if you look at the rating, it has changed to R, and the title is still the same. Now let's do a second condition where if the movie does not exist, it will actually go and create a new entry. So let's go and try that. So to do that I'm just going to use the same movie. So we do a find by Rocky31. So I will actually go and delete this movie, right, I will talk about delete in a minute here, but basically it's the object.delete. So that should delete the movie. So if I do a find by Rocky31, I should get an error so this document, this Rocky31 is not in the database. Basically this ID 005 is not in the database. Okay, now if I do a command recall, so if I try to do a movie.new, Go and find a movie with this ID 005, title Rocky31, rated R, and I do an upsert. Remember we don't have a movie called Rocky31. We don't have a movie with ID 005. I just deleted that. But if I do this, this will successfully insert an entry, right? Because what happened was, it's an upsert if the document does not exist, it actually adds an entry. So now if I go back and do a find by I should see the data. So it's right there, movie rated R Rocky31 005. So we covered both conditions of an upsert, one if its there, it'll update. Second, if it's not there, we go and create and insert a new entry or document into the collection. Let's look at the final operation, delete. Model.delete or movie.delete will delete a document in the database. An uppercase M or collection.delete_all will delete all the documents from the movies collection. So let's go give delete a quick try. Oh, let's first go and create a simple movie. Here, movie.create so that's our, we title Rocky31. Let's first go and do a find. So let's do a find by title. Rocky31, and there is is. A 00D, 00D. So now we can do a movie.delete, and let's do a find by to see again if the data is still there. Okay, and the term, movie.delete, successful. And if I do a command recall and try to find the movie, then the movie, the document does not exist or not found. So fairly simple to do a delete. Just do a delete on the object. Just to summarize this topic, so Mongoid supports all the expected CRUD operations, and we will be looking at this a lot more as we go into relationships and other topics. What's next? We will set up our movie application. And we need to set up for the relationship topic and the final demo. Thank you.