Updated June 28, 2023
Introduction to MongoDB finds by id.
The following article provides an outline for MongoDB find by id. MongoDB provides a function with the name findById(), which is used to retrieve the document matching the ‘id’ specified by the user. To use findById in MongoDB, the find() function is used. If no document matches the specified ‘id’, it returns null. It is one of the important functions which is used widely when there is a need to retrieve the document according to their ids. Though many languages can use the MongoDB database to perform their database task, it also depends on the language specifications as they vary when it comes to the usage of the function.
Syntax of MongoDB finds by id.
Given below is the basic syntax of using the find by id in a MongoDB database:
find() function is used to use find by id in MongoDB.
db.collection_name.find(query, projection)
Where,
- Query: It is an optional parameter. It specifies the selection filter using the operators of the query. This parameter can be omitted to return all the documents in a collection.
- Projection: It is an optional parameter. It specifies the field which will return matching the query filter. This parameter can be omitted to return all the fields in the matching documents.
Return type:
It returns the cursor to the documents which match the query criteria.
How does find by id Work in MongoDB?
- As the name suggests, find by id is used to retrieve the document’s details matching the particular ‘id’ provided by the user, where ‘id’ is the automatically generated ‘id’ when a document is created in the database.
- When the user calls findById(_id) in Mongoose, it automatically calls findOne({_id}), which means that the findById calls findOne in the middle.
- The casting of queries is done in Mongoose to match the schema. ‘Id’ passed by the user is in a string converted to ObjectId by Mongoose.
- The ‘_id’ passed is the automatically generated id when a document is inserted in the database.
- A simple ‘find()’ function can be used to retrieve the automatically generated id without specifying any criteria.
- In Mongosh, db.collection_name.find() 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 documents by specifying the id.
Examples of MongoDB find by id.
Below are examples showing the usage of find by id in a MongoDB database. Consider the following conditions:
- There is a database already present with the name ‘db’.
- There is a collection in the database with the name ‘myCol’.
- All the documents are inserted in the collection ‘myCol’ using the insert function.
Example #1
Using the find() function without any parameters.
Code:
db.myCol.find();
Output:
Explanation:
- In the above example, MongoDB’s find() function is used without passing any parameter. Since none of the parameters is passed, the find() function will retrieve all the detailed documents in the collection. We can see that an additional field of ObjectId is automatically inserted at the start of each document.
- MongoDB inserts the ObjectId for every new document inserted by the user. This object is unique and cannot be the same for two or more documents. It is one of the easiest ways to retrieve the ObjectId of all the documents and use it further according to the specific requirements.
Example #2
Using the find() function and passing the ObjectId parameter in it.
Code:
db.myCol.find({_id : ObjectId("60f532903ded77001064ae92")});
Output:
Explanation:
- In the above example, the document is found based on the ObjectId of the document. ObjectId is the unique id automatically allocated when a new document is inserted into the collection.
- We can provide the ObjectId as a parameter of the find() function in MongoDB, and its respective document is displayed on the console to the user.
Example #3
Using the find() function by passing the parameters of the fields other than the unique objectId.
Code:
db.myCol.find({"name" : "sunita"});
Output:
Explanation:
- As we can see in the above example, the user can also put the parameter of the find function of the other fields of the document.
- As the “name” is also a field on which each document can be filtered, so the document having the “name” as “sunita” is found, and all its details are displayed to the user on the console.
Suppose there are two or more documents in the collections having the same value for the particular field. bLet’s see the situation with the help of the data present in the collection similar to the one given below:
Code:
db.myCol.find();
Output:
Example #4
Using the find() function and passing the field parameter is the same for more than one record.
Code:
db.myCol.find({"city" :"agra"});
Output:
Explanation:
- The above example shows that more than one document is present in the collection with the field “city” as “agra”. So when we pass the parameter of “city”: “agra”, all the documents matching the criteria will be displayed to the user on the console. It is never the case of the find() function that only one record will be retrieved at a time.
- In the case of ObjectId only one document would be displayed to the user as it is unique to each document and can never be the same. Else for the other cases, all the documents satisfying the condition mentioned in the find() parameters would be displayed to the user on the console.
Conclusion
The above description clearly explains what the find by id is and how it works in MongoDB to retrieve the documents according to the specified id by the user, as we know that the find() function is used to retrieve the documents with the given id. The find() function internally calls the findOne() to handle the situation accordingly.
Recommended Articles
This is a guide to MongoDB find by id. Here we discuss the introduction. How does find by id work in MongoDB? And their examples, respectively. You may also have a look at the following articles to learn more –