Hello, I hope you find well, and in these days of isolation I recommend you use your time to learn something new or an affected skill, that’s why I come to bring you the second part of how to use mongo in node js. In my old article we saw how to make a connection to our local mongo database using the js node, and now we will see how we can do a CRUD using these two technologies.
First of all, we are going to define what a CRUD is, it is an acronym with the following meaning: Create, Read, Update and Delete, then below we will make an example of each of the acronym CRUD, these are the basic functions of a database to be able to create a more robust functionality in a system, without more to say let’s begin.
Before starting, I tell you that we are going to use the same files that we used in the previous article to connect to our database in mongodb, I remind you that we need to have our mongodb service turned on, for more details visit the previous article, the project was as follows:
To start we are going to make a method in the app.js file called create, this method will receive the mongo object to be able to use its dependencies and achieve insertion, the mongo method that we will use is “insertOne” this method receives two parameters, one is the information that we are going to insert and the following is a callback that returns an error if there is one and the result of the insertion. So the first thing to do is evaluate if there is an error, if it exists we have to notify the user that there is something wrong, in our case we will only return an error message and end the program, in the same way we will create an object with the name, user and password values, these data make up a document in our collection that we will call users, then I show you how your code should look:
Then we are going to run our file with the command “node app.js” to see what happens. And what we get on our console is the following:
Finally we will assess if our document is in our database:
If you got here, it means that everything is fine, otherwise I recommend you review the article in case there was something wrong when coding, in the same way I remind you that this article is the continuation of another, I recommend that you give a revised one to be able to continue with this article.
Well what follows now is to do a “read”, we are going to look for the document that we inserted previously, for that we must create another method that we will call “read” which will also receive the mongo object and we will use the dependency “findOne”, this method it receives a “criteria”, which is basically the where of a query in sql, then it also receives a callback with an error and the result of our search, as in the previous method we have to validate the error and finally we will paint the result on the screen From our search, it is important to tell you that we must comment on the call to the create method so that it does not run again and instead, we will call our new method “read”, as shown below:
And our result is as follows:
Then what we will do will be the “update” method, which is basically the same as the create, only the mongo instruction would change, which would be “updateOne”, only in this method we have to take into account the record we want to make the update, for this we will have to make a criteria, in this case we will do it with the field “name”, once having the criteria we will put the fields that we want to modify, for example we will change the user and the password and then the method would be from the following way:
If everything goes well, our console should look like this:
And finally our database would look like this:
Finally we will create the “delete” method, which will delete our record in the database, for this we will use the mongo method called “deleteOne” which only receives the criteria and the response callback, then our method would look like this:
And our console would look like this:
Finally, our collection of our database would be empty.
And this would be all, with this we conclude the incorporation of a CRUD using node and mongodb, I hope it will be useful for you and you have learned something or if not at least arouse your curiosity, remember to experiment with the code and make your own improvements to practice , on my part it would be all, see you and remember do not stop learning.