Skip to content

Commit

Permalink
Merge pull request #11186 from Automattic/gh-11071
Browse files Browse the repository at this point in the history
Gh 11071
  • Loading branch information
vkarpov15 committed Jan 9, 2022
2 parents 6098a00 + 979989c commit 8f0bf41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/document.js
Expand Up @@ -2239,7 +2239,9 @@ Document.prototype.isSelected = function isSelected(path) {
if (this.$__.selected == null) {
return true;
}

if (!path) {
return false;
}
if (path === '_id') {
return this.$__.selected._id !== 0;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/model.js
Expand Up @@ -825,7 +825,6 @@ function checkDivergentArray(doc, path, array) {

Model.prototype.$__version = function(where, delta) {
const key = this.$__schema.options.versionKey;

if (where === true) {
// this is an insert
if (key) {
Expand All @@ -834,12 +833,17 @@ Model.prototype.$__version = function(where, delta) {
return;
}

if (key === false) {
return;
}

// updates

// only apply versioning if our versionKey was selected. else
// there is no way to select the correct version. we could fail
// fast here and force them to include the versionKey but
// thats a bit intrusive. can we do this automatically?

if (!this.$__isSelected(key)) {
return;
}
Expand Down
27 changes: 27 additions & 0 deletions test/model.test.js
Expand Up @@ -7562,6 +7562,33 @@ describe('Model', function() {
assert.ok(err == null);

});
it('Using bulkSave should not trigger an error (gh-11071)', async function() {

const pairSchema = mongoose.Schema({
name: String,
timestamp: String
}, { versionKey: false });

const model = db.model('test', pairSchema);
const tests = [
{ name: 't1', timestamp: Date.now() },
{ name: 't2', timestamp: Date.now() },
{ name: 't3', timestamp: Date.now() },
{ name: 't4', timestamp: Date.now() }
];

model.insertMany(tests, {
ordered: false
});
const entries = await model.find({});
for (const p of entries) {
p.timestamp = Date.now();
}

// !!! "TypeError: path.indexOf is not a function" occurs here
const res = await model.bulkSave(entries);
assert.ok(res);
});
});

describe('Setting the explain flag', function() {
Expand Down

0 comments on commit 8f0bf41

Please sign in to comment.