In this last article about MongoDB, we are going to complete our knowledge about how to write queries using this technology. In particular, we are going to focus on how to update documents in a MongoDB environment.
Like in the last article, since I’m gonna create a showcase of the various different queries you can write, my advice for you is to create a playground folder with a playground.js file. This way, you will be able to execute and play around with the code without corrupting the rest of your project.
A query for every goal
I will show you all the different types of update query we can write in a MongoDB environment. Keep in mind that in order to do this, I have created an example student called Mark using the
beforeEach() method. Doing so, before performing any operation we will know that there is such student into the database that is ready to be updated.
Let’s analyze the code
Before analyzing the code in depth, notice the makeAssert function I have created and let’s try to think about why you may want to create such a function. When deciding if it’s necessary to enclose our code in a function, here are some of the factors we should take into account:
- Are we performing the same operation many times?
- Enclosing this code will make it more readable?
- Enclosing this code will make it easier to test?
And the answer in our case is a big YES for basically everything. Here is why:
- Since we are constantly updating our Student called ‘Mark’ and then performing the same exact test on that Student, it would be more appropriate for us to avoid any repetition in code and enclose the test in a function. In that function, we are not only checking that there is only one Student, but also that his name is actually the one we decided when updating ‘Mark’, in our case:
- Now that our code is put in a function, we can for sure admit that what we are trying to do with that code will be easier to understand for future readers of our program. This is because the function has a name that specifically says what the function is doing: making an assertion.
- Now that we have enclosed our code in a single place, whenever we will encounter a problem with it we will instantly be able to check the function and correct it, doing that only one time!
Now that we have clarified that, let’s analyze the different ways in which we can update records in MongoDB. These methods as you might have already imagined, are separated into two main categories like every other query we have performed earlier on in this article’s series. Model instance updating and class-based updating.
Model instance updating
The set() method
When it comes to model instance updating, MongoDB provides us the
set() method which is quite useful. You just need to specify in single quotes the property of a document you want to modify and then specify the new value you want to give to this property. After doing that, we can then perform a
save(), which in our case is located inside the
The instance.update() method
instance.update() method will take care of updating the specified instance with a new value in a very similar way to the
set() method. Just call this method on the desired instance specifying the property you want to update and the value you want to feed to it.
The class-based update() method
Similarly to all the other methods we have seen earlier in this article’s series, the update method will take as a first argument the criteria of the document you want to retrieve, and then the property you want to update and its new value specified.
The findOneAndUpdate() method
This method is quite explanatory by itself already, it will basically work as the
update() method described above, the only difference is that it will only modify the first document retrieved that respects the criteria specified. So in our case, even if we had 10 Student’s called Mark, with this method we would only update the first one that is retrieved.
The findByIdAndUpdate() method
Extremely similar to the method described above, the only difference is that it will search for a document based on it’s _id.
We have definitely gone through a lot with this article’s series. We have explored the basics of MongoDB, its differences related to a classic relational database, how to test code using it and even how to install it and write our first queries. Hope this won’t only be useful in the future for you to be able to perform basic operations using this amazing technology, but maybe you discovered a new way of working with databases that might turn on the desire in you to explore more in the future.