2020-01-27 mongodb

Comparing MongoDB ObjectIds

In MongoDB, the ObjectId is a data type and also a method used to generate unique strings. These strings are usually used as the value of the property _id of each document inside of a collection.


ObjectIds can be effectively compared using the ObjectId().equals() method.

Here is an example

The string 5e2ed1b83f98e15f9799bfd2 is an ObjectId, even though its represented as a regular string. Any attempt of a comparison between two ObjectIds would return false.

What’s happening in the code above?

Two ids are being created with the same string that obeys the ObjectId format 5e2ed3bb8f6ee86b8d4d21b7. In the lines bellow, both constants holding the ObjectId values are printed. If you execute the script above, you will see that the terminal will print the same string twice:

And then, in the next line, when a comparison between the two ObjectId is made, the terminal prints false.

If you pay attention more closely, you will see that a comparison between the two ObjectIds would not be possible, because both constants are holding an Object, despite the fact they print a string when logged.

The proper way to compare ObjectId’s

Each ObjectId object has a method called .equals(), this method receives another ObjectId object as an argument and then compares its value to the value of the ObjectId where the .equals() has been called.