refactor: use Object.hasOwn instead of Object#hasOwnProperty#15875
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the codebase to use the modern Object.hasOwn() API instead of the legacy Object.prototype.hasOwnProperty.call() and .hasOwnProperty() patterns. This is a port of #15866 to the 8.x branch, bringing consistency and modernization to property existence checks across the codebase.
Key changes:
- Replaced all instances of
.hasOwnProperty()andObject.prototype.hasOwnProperty.call()withObject.hasOwn() - Removed the custom utility function
exports.object.hasOwnPropertyfromlib/utils.js - Applied optional chaining (
?.) in some locations to simplify null checks
Reviewed changes
Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/virtualType.js | Updated property check to use Object.hasOwn() |
| lib/utils.js | Removed deprecated hasOwnProperty utility function and updated hasUserDefinedProperty |
| lib/types/objectid.js | Updated prototype property check |
| lib/types/documentArray/index.js | Converted proxy handler property checks and added optional chaining |
| lib/types/array/index.js | Converted proxy handler property checks and added optional chaining |
| lib/schemaType.js | Updated constructor and instance property checks |
| lib/schema/array.js | Updated type checking logic |
| lib/schema.js | Converted numerous property existence checks across schema operations |
| lib/query.js | Updated option and discriminator key checks |
| lib/mongoose.js | Updated model caching checks and added optional chaining |
| lib/model.js | Updated session option checks and hook context checks |
| lib/helpers/update/decorateUpdateWithVersionKey.js | Simplified hasKey helper function |
| lib/helpers/update/applyTimestampsToUpdate.js | Updated timestamp update checks |
| lib/helpers/timestamps/setupTimestamps.js | Updated currentTime option check |
| lib/helpers/setDefaultsOnInsert.js | Updated path existence checks |
| lib/helpers/query/getEmbeddedDiscriminatorPath.js | Updated array filter checks |
| lib/helpers/projection/applyProjection.js | Updated projection property checks |
| lib/helpers/populate/*.js | Updated various populate-related property checks |
| lib/helpers/model/castBulkWrite.js | Updated discriminator key check |
| lib/helpers/model/applyMethods.js | Updated method conflict check |
| lib/helpers/indexes/*.js | Updated index-related property checks |
| lib/helpers/common.js | Updated nested path check |
| lib/drivers/node-mongodb-native/connection.js | Updated option and collection property checks |
| lib/document.js | Extensive updates to property checks and refactored filter chain to for loop |
| lib/connection.js | Updated config, session, and collection property checks |
| lib/cast.js | Updated schema path check |
| lib/aggregate.js | Updated session option check |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
hasezoey
approved these changes
Dec 5, 2025
vkarpov15
approved these changes
Dec 5, 2025
Collaborator
vkarpov15
left a comment
There was a problem hiding this comment.
Works for me. Just for completeness, Object.hasOwn() has been in Node.js since 16.9.0, and Mongoose 8 requires >=16.20.1 so this is safe.
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.
Port #15866 to 8.x