-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: make a few micro-optimizations to help speed up findOne() #15022
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -22,7 +22,6 @@ const castUpdate = require('./helpers/query/castUpdate'); | |||||
const clone = require('./helpers/clone'); | ||||||
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue'); | ||||||
const helpers = require('./queryHelpers'); | ||||||
const immediate = require('./helpers/immediate'); | ||||||
const internalToObjectOptions = require('./options').internalToObjectOptions; | ||||||
const isExclusive = require('./helpers/projection/isExclusive'); | ||||||
const isInclusive = require('./helpers/projection/isInclusive'); | ||||||
|
@@ -2248,6 +2247,9 @@ Query.prototype.error = function error(err) { | |||||
*/ | ||||||
|
||||||
Query.prototype._unsetCastError = function _unsetCastError() { | ||||||
if (this._error == null) { | ||||||
return; | ||||||
} | ||||||
if (this._error != null && !(this._error instanceof CastError)) { | ||||||
return; | ||||||
} | ||||||
|
@@ -2291,9 +2293,9 @@ Query.prototype.mongooseOptions = function(v) { | |||||
|
||||||
Query.prototype._castConditions = function() { | ||||||
let sanitizeFilterOpt = undefined; | ||||||
if (this.model != null && utils.hasUserDefinedProperty(this.model.db.options, 'sanitizeFilter')) { | ||||||
if (this.model != null && this.model.db.options?.sanitizeFilter != null) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also be shortened to:
Suggested change
|
||||||
sanitizeFilterOpt = this.model.db.options.sanitizeFilter; | ||||||
} else if (this.model != null && utils.hasUserDefinedProperty(this.model.base.options, 'sanitizeFilter')) { | ||||||
} else if (this.model != null && this.model.base.options?.sanitizeFilter != null) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here
Suggested change
|
||||||
sanitizeFilterOpt = this.model.base.options.sanitizeFilter; | ||||||
} else { | ||||||
sanitizeFilterOpt = this._mongooseOptions.sanitizeFilter; | ||||||
|
@@ -2536,13 +2538,12 @@ Query.prototype.collation = function(value) { | |||||
* @api private | ||||||
*/ | ||||||
|
||||||
Query.prototype._completeOne = function(doc, res, callback) { | ||||||
Query.prototype._completeOne = function(doc, res, projection, callback) { | ||||||
if (!doc && !this.options.includeResultMetadata) { | ||||||
return callback(null, null); | ||||||
} | ||||||
|
||||||
const model = this.model; | ||||||
const projection = clone(this._fields); | ||||||
const userProvidedFields = this._userProvidedFields || {}; | ||||||
// `populate`, `lean` | ||||||
const mongooseOptions = this._mongooseOptions; | ||||||
|
@@ -2643,7 +2644,7 @@ Query.prototype._findOne = async function _findOne() { | |||||
// don't pass in the conditions because we already merged them in | ||||||
const doc = await this.mongooseCollection.findOne(this._conditions, options); | ||||||
return new Promise((resolve, reject) => { | ||||||
this._completeOne(doc, null, (err, res) => { | ||||||
this._completeOne(doc, null, options.projection, (err, res) => { | ||||||
if (err) { | ||||||
return reject(err); | ||||||
} | ||||||
|
@@ -3238,7 +3239,7 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop, | |||||
|
||||||
function _init(err, casted) { | ||||||
if (err) { | ||||||
return immediate(() => callback(err)); | ||||||
return callback(err); | ||||||
} | ||||||
|
||||||
|
||||||
|
@@ -3251,12 +3252,12 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop, | |||||
} else { | ||||||
res.value = null; | ||||||
} | ||||||
return immediate(() => callback(null, res)); | ||||||
return callback(null, res); | ||||||
} | ||||||
if (options.session != null) { | ||||||
casted.$session(options.session); | ||||||
} | ||||||
immediate(() => callback(null, casted)); | ||||||
callback(null, casted); | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -3465,7 +3466,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() { | |||||
const doc = !options.includeResultMetadata ? res : res.value; | ||||||
|
||||||
return new Promise((resolve, reject) => { | ||||||
this._completeOne(doc, res, (err, res) => { | ||||||
this._completeOne(doc, res, options.projection, (err, res) => { | ||||||
if (err) { | ||||||
return reject(err); | ||||||
} | ||||||
|
@@ -3561,7 +3562,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() { | |||||
const doc = !includeResultMetadata ? res : res.value; | ||||||
|
||||||
return new Promise((resolve, reject) => { | ||||||
this._completeOne(doc, res, (err, res) => { | ||||||
this._completeOne(doc, res, options.projection, (err, res) => { | ||||||
if (err) { | ||||||
return reject(err); | ||||||
} | ||||||
|
@@ -3715,7 +3716,7 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() { | |||||
|
||||||
const doc = !includeResultMetadata ? res : res.value; | ||||||
return new Promise((resolve, reject) => { | ||||||
this._completeOne(doc, res, (err, res) => { | ||||||
this._completeOne(doc, res, options.projection, (err, res) => { | ||||||
if (err) { | ||||||
return reject(err); | ||||||
} | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vkarpov15 Since you already checked
(this._error == null)
above, you can simplify the condition as below.if (!(this._error instanceof CastError)) {}