How to Find by ID in MongoDB

Tahseen Tauseef Feb 15, 2024
  1. MongoDB find by id() Function
  2. Examples of MongoDB find by id()
  3. the find by ObjectId() Method in MongoDB
  4. the find by name Method in MongoDB
How to Find by ID in MongoDB

The following article outlines the MongoDB find by Id() method. MongoDB provides a function find by Id(), which retrieves the document matching the user’s id.

To use find by Id() in MongoDB, the find() function is used. It returns a null value if no document matching the specified id is found.

It is one of the essential functions widely used when it is necessary to retrieve the document according to its id.

MongoDB find by id() Function

The user will use the find() function to use find by id() in MongoDB. Below is the syntax of using the find by id() function in a MongoDB database.

db.collection_name.find(query, projection)
  1. query - It is an optional parameter. It uses the query operators to specify the selection filter.

    If this parameter is ignored, all documents in a collection will be returned.

  2. projection - It is also an optional parameter. It provides the field returned if the query filter is met.

    All fields in the matching documents will be returned if this option is omitted.

  3. Return type - returns the cursor to the documents that match the search criteria.

The find by id() retrieves the document’s details matching the specific id specified by the user, where id is the automatically produced id when a document is created in the database, as the name implies.

Similarly, the _id parameter is the id produced automatically when a document is placed into the database.

A simple find() function will be used without specifying any criteria to access the automatically produced id. In Mongosh, the db.collection_name.find() function will automatically iterate the cursor and display up to the first 20 documents of the collection.

Different languages use different ways to retrieve or find the documents by specifying the id.

Examples of MongoDB find by id()

Below are some examples showing the usage of find by id() in a MongoDB database. In addition, there is a collection in the database with the name teams, which will be used for all the examples in this article.

db={
  "teams": [
    {
      team: "Manchester City ",
      position: "1st",
      points: 70
    },
    {
      team: "Liverpool",
      position: "2nd",
      points: 69
    },
    {
      team: "Chelsea",
      position: "3rd",
      points: 59
    },
    {
      team: "Arsenal",
      position: "4th",
      points: 54
    },
    {
      team: "Tottenham",
      position: "5th",
      points: 51
    },
    {
      team: "Manchester United",
      position: "6th",
      points: 50
    }

  ]
}

In this example, you will use the find() function without any parameters. The code for this query is given below.

db.teams.find();

The screenshot for the output of the above query is given below.

find by id

You can access the link for this example here.

In the example given above, the find() function of MongoDB is used without passing any parameter. Since none of the parameters is passed, the find() function will retrieve all the detailed documents present in the collection.

Furthermore, we can see that an additional field of ObjectId() is automatically inserted at the start of each document.

MongoDB inserts the ObjectId() for each new document inserted by the user. This ObjectId() is one-of-a-kind and cannot be used in more than one document.

It’s one of the simplest ways to get all of the documents ObjectId() and use them according to the specific requirements.

the find by ObjectId() Method in MongoDB

You will use the find() function and pass the ObjectId() parameter in it.

The code for this query is given below.

db.teams.find({
  _id: ObjectId("5a934e000102030405000002")
})

The screenshot for the output of the above query is given below.

find by object id

In the example above, the document is found based on the document’s ObjectId(). ObjectId() is the unique id allocated automatically when a new document is inserted into a collection.

In MongoDB, you can pass the ObjectId() parameter to the find() function and the user will see the corresponding document on the console.

the find by name Method in MongoDB

You will use the find() function by passing the parameters of the fields other than the unique ObjectId.

The code for this query is given below.

db.teams.find({
  "team": "Arsenal"
})

The screenshot for the output of the above query is given below.

find by id 2

As we can see in the example given above, the user can also put the parameter of the find() function of the other fields of the document.

As the team is also a field on which each document can be filtered, the document having a team like Arsenal is found, and all its details are displayed to the user on the console.

This MongoDB article described the find by Id() function and how it works in MongoDB to retrieve documents based on the user-specified id.

As we all know, the find() method is used to retrieve documents based on the provided id. Internally, the find() method invokes findOne() to deal with the circumstance.