{"success":true,"name":"comparing-mongodb-objectids-with-always-fails-use-equals-or-","description":"Using MongoDB and two 'identical' IDs refuse to match in code","category":"ai-brimind","path":"ai-brimind/comparing-mongodb-objectids-with-always-fails-use-equals-or-/SKILL.md","file":"SKILL.md","content":"---\nname: comparing-mongodb-objectids-with-always-fails-use-equals-or-\ndescription: \"Using MongoDB and two 'identical' IDs refuse to match in code\"\n---\n\n# Comparing MongoDB ObjectIds With === Always Fails: Use .equals() or Compare Strings\n\n## When to use\nProject uses MongoDB/Mongoose, and ID comparisons (ownership checks, filtering, deduplication) fail or behave inconsistently though the printed values look the same.\n\n## Example request\ndoc.userId === currentUser._id → two ObjectId objects, reference comparison, always false; the equality trap in Mongo clothing. filter comparing a populated document to an ID → one side became a full object after populate; apples versus fruit baskets. an ID from the request (string) compared to a database ID (ObjectId) → types differ, strict equality says no; cross-type mismatch. — each compared identity cards while asking about the person.\n\n## Instructions\nAct as a records clerk who knows two photocopies of the same certificate are still two pieces of paper — comparing the papers answers nothing about the certificate. ObjectIds are objects, and JavaScript's strict equality on objects compares references, per the general equality rules — so two ObjectIds holding the identical ID are 'not equal' unless they are literally the same object in memory, which after separate queries they never are; the bug's signature is an ownership check or filter that fails while console output shows two visibly identical IDs. The correct tools: the .equals() method that ObjectIds carry (idA.equals(idB) — which also gracefully accepts a string as the argument), or normalize both sides to strings (String(id) or its toString) before comparing — pick one convention per the project's consistency rules and use it everywhere IDs meet. Know the three disguises this bug wears: request-versus-database (URL params arrive as strings per the form-string rules, database IDs are objects); populated-versus-raw (after populate, the field holds a full document — compare against its _id, and expect the null case per the dangling-reference rules); and deduplication or membership checks (a Set of ObjectIds never detects duplicates — build the Set from strings). Validate incoming ID strings before querying, per the ObjectId-validation rules. When an ID comparison lies, print both sides with their types — the mismatch is visible immediately. Only proceed once every ID comparison in the change uses .equals() or string-normalized values, never bare strict equality between objects.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #829\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}