mongoose findById
Introduction
One of the most frequently used CRUD operations is the Read operation. Mongoose provides several methods to read the data from a database. Each method has its own specialty. We can use these methods according to our requirements. One of these methods is the findById() method. As the name suggests, the findById() method is used to find a document by its id. By id, it means the auto-generated _id field of each document. It is easy to find a document by its id in MongoDB because it acts as the primary key and is unique throughout the collection. In this article, we will discuss how to use the mongoose findById() method.
Observe the following collection.
1 2 3 4 5 | { "_id" : ObjectId("5e463e67e6f8b824961e87cc"), "name" : "Leonel Messi", "club" : "Barcelona", "country" : "Argentina" } { "_id" : ObjectId("5e463ea3e6f8b824961e87cd"), "name" : "Cristiano Ronaldo ", "club" : "Juventus", "country" : "Portugal" } { "_id" : ObjectId("5e463eb3e6f8b824961e87ce"), "name" : "Neymar", "club" : "PSG", "country" : "Brazil" } { "_id" : ObjectId("5e463ecee6f8b824961e87cf"), "name" : "Sergio Ramos", "club" : "Real Madrid", "country" : "Spain" } { "_id" : ObjectId("5e463ef9e6f8b824961e87d0"), "name" : "Marc-Andre ter Stegen", "club" : "Barcelona", "country" : "Germany" } |
The name of this collection is “myteam”. We can see that the value of the _id field is different in each document. Check the string that is passed to the ObjectId() method. We will use this string to match a document.
We will use the postman tool for testing. You can download it from www.getpostman.com.
findById()
So let’s start by creating a route handler.
1 | router.route("/find").get(function(req, res) {}); |
The above route handler will be invoked when the route ‘/find’ is executed. Let’s add the findById() method in it.
1 2 3 4 5 6 7 8 9 10 11 12 | router.route('/find').get(function(req,res){ myteam.findById( , function(err, result) { if(err) { console.err(err); } else { res.json(result) } }) }) |
So currently, we are passing nothing to the findById() method. We need to match a document by id. Observe the following document.
1 | { "_id" : ObjectId("5e463ecee6f8b824961e87cf"), "name" : "Sergio Ramos", "club" : "Real Madrid", "country" : "Spain" } |
Here, the id is “5e463ecee6f8b824961e87cf”. We will fetch this document using its id. To do this, we just need to pass the value to the findById() method.
1 2 3 4 5 6 7 | myteam.findById("5e463ecee6f8b824961e87cf", function(err, result) { if (err) { console.err(err); } else { res.json(result); } }); |
We have to pass the id as a string. Let’s execute this route using the postman tool.
1 2 3 4 5 6 7 8 9 | router.route("/find").get(function(req, res) { myteam.findById("5e463ecee6f8b824961e87cf", function(err, result) { if (err) { console.err(err); } else { res.json(result); } }); }); |
The findById() method returns a single document. This method is very similar to the find() method if the query passed to it is in the following way.
1 2 3 | { _id: "5e463ecee6f8b824961e87cf"; } |
Let’s replace the findById() method with the find() method.
1 2 3 4 5 6 7 8 9 | router.route("/find").get(function(req, res) { myteam.find({ _id: "5e463ecee6f8b824961e87cf" }, function(err, result) { if (err) { console.err(err); } else { res.json(result); } }); }); |
Let’s execute this route using the postman tool.
We have the same result. The only difference is that the findById() method returns an object while the find() method returns an array of objects.
Conclusion
This is the findById() method works. It returns a single object. It is very useful when we need to find a single document using the value of the _id field. Moreover, we also discussed how we can use the much popular, the find() method in a similar way.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started