In MongoDB, it is quite easy and effective to interact with documents by using unique IDs. The find() method of MongoDB can also be applied on a MongoDB collection, by referring to the IDs of the documents. The whole process in which documents are retrieved by using unique IDs in the find() method is named as “find() by id”.
In this post of MongoDB series, we will provide a sequential guide to apply find() method to retrieve documents by id’s.
How find by id method works in MongoDB
In MongoDB, the find() by id method is an extension of find() method and therefore the primary syntax used is the same as of find() method. The syntax to apply find() by id is given below:
In MongoDB, there are two possibilities for a unique id:
- If the user defines id value while inserting the documents then it must be unique.
- If the user does not create a unique id, the MongoDB automatically generates it uniquely for each document.
How to use find by id method in MongoDB
Before starting the application of find by id on a collection; the following MongoDB based instances will be used in this tutorial:
Database name: “linuxhint” is the database name used in this guide
Collection(s)-name: Two collections of “linuxhint” database are used that are named as “distributions” and “employees“.
Example 1: User-defined id
The “distributions” collection will be used in this example. As mentioned earlier, when user insert each document with “_id” field then it becomes user defined unique id’s: For instance, the documents inserted in “distributions” collection contains user defined id’s (1,2,3…) as can be seen in the output below:
You can retrieve any document by reference its id in the find() method. For example, the command written below will help to retrieve a document that has “_id” value 2:
Similarly, you can get any other document by using the “find by id” method.
Using find by id and sort methods on user-defined ids: Moreover, with the help of the find() method and sort() method, you can get the output in ascending order of ids.
The command mentioned below shows the application of the “find” and “sort” methods on ids of “distributions” collection.
Note: the sorting order can be “1” or “-1,” which stands for ascending or descending respectively.
Example 2: System defined id
Here, in this example, “employees” collection is used, and this collection contains documents that have system defined “ids” as shown in the output below:
You will see that the “_id” field contains a complex, unique id for each document.
So, to retrieve any document, you must pass the long id as shown in the command below:
Using find by id and sort methods on system-defined ids: Like on user-defined id; you can use the sort method on system-defined id’s to get the output in either ascending or descending order:
The command written below will sort the documents of “employees” collection in descending order:
Note: The syntax is the same, but the collection name is different, and the id’s definition is also different.
While dealing with system-defined id’s, if you mistakenly inserted the wrong length of “_id,” then you may encounter the following error:
Or if you want to retrieve system defined “id’s” by user-defined “id“, the command will be executed but will not show any output because user-defined id’s do not exist on “employees” collection:
Conclusion
The find() method of MongoDB contains an extensive list of supported operators and commands that help to retrieve documents in a refined form. The unique id can be used with the find() method to get the documents based on their IDs. By following this guide, Mongo users can get the documents by using the id of those documents in the find() method. Moreover, for better understanding, a few examples are provided that show the usage of the “find() by id” method in MongoDB.