Mongoose: ObjectId Comparisons fail inconsistently

A straight == (or ===) comparison will compare the two objects by reference, not value. So that will only evaluate to true if they both reference the very same instance.

Instead, you should be using the equals method of ObjectID to compare their values:

story._id.equals(offref.ref)

As @bendytree notes in the comments, if either value could be null (and you want nulls to compare as equal), then you can use the following instead:

String(story._id) === String(offref.ref)

Leave a Comment