We can use NodeJS with SQL and NoSQL databases like MySQL and MongoDB both alike. On that note, in this article, we will look at how to find a document by its ID using MongoDB and the Mongoose library. The Mongoose library is an object document mapper that allows us to define objects with a strongly typed schema that can be mapped to a MongoDB document. It also provides certain functions for CRUD activities. Thus, now let us look at the findByID MongoDB function.

Why do we use MongoDB? Read this to know !! ~~> ~~>

The findByID() function ~~> ~~>

The findById() function is used to find a single document based on its _id field. The _id field is cast based on the schema before the command is sent:

  • Open the install mongoose module link.
  • Install the mongoose module using:
npm install mongoose
  • After that, you can simply create a folder and add a file, for example index.js. You can run that by:
node index.js
findByID MongoDB
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://', {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
// User model
const User = mongoose.model('User', {
    fname: { type: String },
    age: { type: Number }
// Finding a document whose id = 5d715a23c452f34e335d2d8d
var id = '5d715a23c452f34e335d2d8d';
User.findById(id, function (err, docs) {
    if (err){
        console.log("Result : ", docs);

How to execute the program?

  • Make sure you have installed the mongoose module with the following command:
npm install mongoose
  • Below you will see the sample data in the database before running the function. I have used the Robo3T GUI tool as shown below:

  • Run the index.js file with the following command:
node index.js

Want to check the version of your MongoDB application? Here’s how it is done~>

findByID MongoDB and Mongoose ~~> ~~>

As the name suggests, the findByID method is used to find documents by specifying a particular ID. By ID, we mean the automatically created _id field when a document is created. The _id field is a unique value. So, it is possible to use the value of _id to find the document.

findByID MongoDB

We have a details collection with three documents:

{ "_id" : ObjectId("5d71522ab352f78f435d2d8b"), "fname" : "Shubham", "age" : 22, "place" : "Delhi" }
{ "_id" : ObjectId("5d715223c476f78e335d2d8c"), "fname" : "Kavin", "age" : 27, "place" : "Bijnor" }
{ "_id" : ObjectId("5d715a23c452f34e335d2d8d"), "fname" : "Ayush", "age" : 29, "place" : "Jaipur" }

If we want to use the findByID function in Mongoose, we need to pass the ID of a specific document. So, now let us see how to make use of the findByID function to retrieve the first document in the details collection.

.then(doc = > { 
.catch(err = > { 

This is the process we need to follow:

  • Define the schema for MongoDB using the schema details.
  • Let us see how we can define the documents in the details collection.
const mongoose = require("mongoose");
const detailsSchema = mongoose.Schema({
  fname: String,
  age: Number,
  place: String
module.exports = mongoose.model("Details", detailsSchema);
  • We create a schema called details Schema that can be exported with the name “details”. Now, let us see the application of the findByID function:
  • Note the parameter of the findById function, what do you see? Yup, it’s the exact as in the _id. ~~> ObjectId(“5d71522dc452f78e335d2d8b”);
  • Also, remember that if you pass only the actual ID, i.e. 5d71522dc452f78e335d2d8b, it will not work.
  • The function returns a promise, and we need to handle it accordingly as given below:
  • Now, if this operation executes successfully then; the received argument – doc – contains the returned document, otherwise an error ensues.

Working without Mongoose ~~> ~~>

Remember that you do not necessarily have to use Mongoose. You can thus always use the tried and true MongoDB. To find a document by ID, you can use the find or findOne methods and specify the ID as an argument:

collection.find({ _id: ObjectId("5d71522dc452f78e335d2d8b") });

#You can even use the below command :
collection.findOne({ _id: ObjectId("5d71522dc452f78e335d2d8b") });

Here’s a quality read comparing MongoDB with MySQL !! ~~> ~~>

WRAPPING UP !! findByID MongoDB ~~> ~~>

The findByID MongoDB function is useful when retrieving data using the _id field. It actually triggers the findOne() function. It returns a promise and we shall handle it accordingly. In this post, you also have seen how to do this, and I hope you can apply what you have learned to your code. Hope this helps. Keep practicing and do well. On that note, until next time, see ya!! Goodbye!! ~~> ~~>

:: ~~> ~~> :: ** :::::: ** :: )) ** :: ** (( ~~> ~~>

Categorized in: