Repository navigation
fix(map): support ObjectId keys in MongooseMap#has() - #16634
Merged
vkarpov15 merged 1 commit intoNov 1, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
TypeScript declarations still restrict Map.has() to string keys, preventing typed callers from using the new runtime ObjectId-key support and requiring an updated signature with a regression test.
1 open issue
What changed in this PR
This PR makes MongooseMap#has() recognize ObjectId keys, matching the key conversion already used by get(), set(), and delete().
Changes:
- Converts ObjectId keys to strings before checking map membership.
- Adds regression coverage for existing, missing, newly inserted, and deleted keys, while checking string-key behavior.
| File | Description |
|---|---|
test/types.map.test.js |
Tests ObjectId membership checks and string-key compatibility. |
lib/types/map.js |
Adds and documents the has() override. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
MongooseMap#get(),set()anddelete()convert ObjectId keys to strings (added in #9938 for #9936), and theset()docs say maps support strings and ObjectIds as keys.has()is not overridden, so the nativeMap#haslooks up the ObjectId instance itself and returns false for a key thatget()finds:This PR adds a
has()override that converts ObjectId keys the same wayget()does, then callssuper.has(). String keys are unchanged.Examples
Added a test in
test/types.map.test.js.npx mocha --exit test/types.map.test.js(65 passing). The new test fails on master withfalse !== trueonhas(userId).npx eslint lib/types/map.js test/types.map.test.jsis clean.