Follow Us on Twitter

How to compare Mongo _id’s in Javascript

by Patrick Sinke on October 15, 2017 · 10 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
Ratings:
VN:D [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

{ 10 comments… read them below or add one }

euservr October 17, 2017 at 10:03 am

Thanks for this Information. It helped me a lot.

Reply

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

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

Reply

Prince April 25, 2018 at 4:41 pm

Man, you saved me a lot of time

Reply

Keondo May 16, 2018 at 6:10 am

Thank you for the information!

Reply

Rej August 12, 2018 at 2:42 pm

Thanks a lot boy

Reply

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

Thank you very much for this!

Reply

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.

Reply

Patrick May 22, 2019 at 7:50 am

Thanks Dan, for the additional info.
Patrick

Reply

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 = req.user.id;
console.log(id1);
var id2 = dish.comments.id(req.params.commentId).author.id;
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’ );

Reply

Arek August 24, 2019 at 1:34 pm

Sorry for the mix-up!
My mistake,
I should have used req.user._id (NOT req.user.id) and
xxx.author._id (NOT xxx.author.id).
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.

Reply

Leave a Comment

 

Previous post:

Next post:

About Whitehorses
Company profile
Services
Technology

Whitehorses website

Home page
Whitebooks
Jobs

Follow us
Blog post RSS
Comment RSS
Twitter