Skip to content

BUG: no difference detected when comparting two mongoose documents (ObjectId). #103

Open
@soknifedev

Description

@soknifedev

When comparing two mongoose models whose _id or discriminated docs arent equal, the library returns no differences.

Steps to reproduce: (pseudo code)

1.Find a document

const original = await exampe.findOne(...);
// original.referencedDoc has value = new ObjectId("667c39c3ef67892912314323");
  1. Perform an update and find document again
await original.update({ referencedDoc: new ObjectId("64c7cda1db76ef25cfc5f901"); })
const latestDocument = await exampe.findOne(...);
  1. Compare differences between original and latestDocument.
const diffs = diff(original, latestDocument);

console.log('diff', diffs) 

Output: (prints no differences)
{}

Expected output: (should print the difference)


{
 referencedDoc: new ObjectId("64c7cda1db76ef25cfc5f901");
}

This bug may be fixed by changing the isEmpty utility function:

const isEmpty = o => Object.keys(o).length === 0;

It is returning zero because "ObjectId" is a class without "keys", but it can be readed as a string perfectly.

Activity

hugs7

hugs7 commented on May 11, 2025

@hugs7

MongoDB Object ids have to be compared with .equals method. I'd say this library probably doesn't handle this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @soknifedev@hugs7

        Issue actions

          BUG: no difference detected when comparting two mongoose documents (ObjectId). · Issue #103 · mattphillips/deep-object-diff