Skip to content
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(model): make insertMany() lean option skip hydrating Mongoose docs #14376

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3430,6 +3430,13 @@ Model.$__insertMany = function(arr, options, callback) {
const results = ordered ? null : new Array(arr.length);
const toExecute = arr.map((doc, index) =>
callback => {
// If option `lean` is set to true bypass validation and hydration
if (lean) {
// we have to execute callback at the nextTick to be compatible
// with parallelLimit, as `results` variable has TDZ issue if we
// execute the callback synchronously
return immediate(() => callback(null, doc));
}
if (!(doc instanceof _this)) {
try {
doc = new _this(doc);
Expand All @@ -3440,13 +3447,6 @@ Model.$__insertMany = function(arr, options, callback) {
if (options.session != null) {
doc.$session(options.session);
}
// If option `lean` is set to true bypass validation
if (lean) {
// we have to execute callback at the nextTick to be compatible
// with parallelLimit, as `results` variable has TDZ issue if we
// execute the callback synchronously
return immediate(() => callback(null, doc));
}
doc.$validate({ __noPromise: true }, function(error) {
if (error) {
// Option `ordered` signals that insert should be continued after reaching
Expand Down Expand Up @@ -3510,7 +3510,7 @@ Model.$__insertMany = function(arr, options, callback) {
callback(null, []);
return;
}
const docObjects = docAttributes.map(function(doc) {
const docObjects = lean ? docAttributes : docAttributes.map(function(doc) {
if (doc.$__schema.options.versionKey) {
doc[doc.$__schema.options.versionKey] = 0;
}
Expand Down Expand Up @@ -3572,6 +3572,9 @@ Model.$__insertMany = function(arr, options, callback) {
return !isErrored;
}).
map(function setIsNewForInsertedDoc(doc) {
if (lean) {
return doc;
}
doc.$__reset();
_setIsNew(doc, false);
return doc;
Expand All @@ -3588,9 +3591,11 @@ Model.$__insertMany = function(arr, options, callback) {
return;
}

for (const attribute of docAttributes) {
attribute.$__reset();
_setIsNew(attribute, false);
if (!lean) {
for (const attribute of docAttributes) {
attribute.$__reset();
_setIsNew(attribute, false);
}
}

if (rawResult) {
Expand Down
7 changes: 6 additions & 1 deletion test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,12 @@ describe('connections:', function() {
it('should not throw an error when attempting to mutate unmutable options object gh-13335', async function() {
const m = new mongoose.Mongoose();
const opts = Object.preventExtensions({});
const conn = await m.connect('mongodb://127.0.0.1:27017/db?retryWrites=true&w=majority&readPreference=secondaryPreferred', opts);

const uri = start.uri.lastIndexOf('?') === -1 ?
start.uri + '?retryWrites=true&w=majority&readPreference=primaryPreferred' :
start.uri.slice(0, start.uri.lastIndexOf('?')) + '?retryWrites=true&w=majority&readPreference=primaryPreferred';

const conn = await m.connect(uri, opts);
assert.ok(conn);
});
});
Expand Down
Loading