Follow Us on Twitter

How to compare Mongo _id’s in Javascript

by Patrick Sinke on October 15, 2017 · 13 comments

When comparing MongoDB generated _id fields in Javascript, you’ll soon or later find out that the following code will not give expected results:

var id1 = mongoose.Types.ObjectId('58a0625246cb87062919eb62');
var id2 = mongoose.Types.ObjectId('58a0625246cb87062919eb62');
if (id1===id2) {
 console.log('elements are equal but we dont get here!' );

Although this appears a valid comparison, the console.log will never be printed. ObjectId’s are objects, and although when printed to the console appear as ordinary text, they cannot be simply compared.

A simple solution is using the .equals() function, which is part of the MongoDB driver:

var id1 = mongoose.Types.ObjectId('58a0625246cb87062919eb62');
var id2 = mongoose.Types.ObjectId('58a0625246cb87062919eb62');
if (id1.equals(id2)) {
 console.log('elements are equal' );



How to compare Mongo _id's in Javascript, 5.0 out of 5 based on 1 rating
VN:D [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

{ 13 comments… read them below or add one }

euservr October 17, 2017 at 10:03 am

Thanks for this Information. It helped me a lot.


Luis Germán March 4, 2018 at 6:26 am

Thank you very much! You saved me a lot of time.


Prince April 25, 2018 at 4:41 pm

Man, you saved me a lot of time


Keondo May 16, 2018 at 6:10 am

Thank you for the information!


Rej August 12, 2018 at 2:42 pm

Thanks a lot boy


Ivan Sardelić November 14, 2018 at 8:23 pm

Thank you very much for this!


Dan May 21, 2019 at 5:07 pm

Also important to note that even the less-strict `==` comparison doesn’t work for ObjectId’s. You have to either use the `.equals()` method mentioned above, or you can directly compare the string representations, e.g. `(id1.toString() === id2.toString)` will return true.


Patrick May 22, 2019 at 7:50 am

Thanks Dan, for the additional info.


Arek August 22, 2019 at 3:37 pm

Hi Patrick,
Thanks for posting about weirdness of ObjectId’s.
In my app, when I use .equals() , it throws me an error .
However, the solution proposed by Dan works, id1.toString() ===id2.toString().
What is going on, any idea why equals() method does not work?
Thanks. Arek

a piece of my code below:

var id1 =;
var id2 =;
console.log(id2); // so I am sure id’s are identical
if (id1.equals(id2)) { // THIS DOES NOT WORK
console.log(‘elements are equal’ );
if (id1.toString() === id2.toString()) { // THIS WORKS
console.log(‘elements are equal’ );


Arek August 24, 2019 at 1:34 pm

Sorry for the mix-up!
My mistake,
I should have used req.user._id (NOT and (NOT
Now the equals() method works as expected!
Also, maybe you need to be careful when using toString() for comparing ObjectId’s as it looks this method maybe less discerning.


Legitimate Online Custom Essay Services November 12, 2019 at 3:24 pm

Online Custom Essay Services are affordable, with rates that are relatively lower in comparison with other Legitimate Custom Essay Writing Services since they allow learners to estimate charges when placing Legitimate Custom Writing Service Orders.


Igor Tatarinov January 25, 2020 at 4:45 am

thank you sir


Accounting Writing Services June 23, 2020 at 7:00 am

Accounting paper writing services are essential and they have become very popular for those seeking accounting coursework writing services since most of them seek Accounting Writing Services.


Leave a Comment


Previous post:

Next post:

About Whitehorses
Company profile

Whitehorses website

Home page

Follow us
Blog post RSS
Comment RSS