Skip to content

Commit

Permalink
Correct shardkey access in buildBulkWriteOps
Browse files Browse the repository at this point in the history
buildBulkWriteOperations adds the shard key to
the filter condition, but it sets it to the
schema's value, not the document's value. It also
updates a problematic test, which passed because
the update value it uses was the same schema's
shard key value.
  • Loading branch information
adf0nt3s committed Jul 18, 2024
1 parent 93ebbe1 commit 693398f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3631,7 +3631,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
const len = paths.length;

for (let i = 0; i < len; ++i) {
where[paths[i]] = shardKey[paths[i]];
where[paths[i]] = document[paths[i]];
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6365,9 +6365,9 @@ describe('Model', function() {
describe('buildBulkWriteOperations() (gh-9673)', () => {
it('builds write operations', async() => {


const userSchema = new Schema({
name: { type: String }
name: { type: String },
a: { type: Number }
}, { shardKey: { a: 1 } });

const User = db.model('User', userSchema);
Expand All @@ -6386,7 +6386,7 @@ describe('Model', function() {
const desiredWriteOperations = [
{ insertOne: { document: users[0] } },
{ insertOne: { document: users[1] } },
{ updateOne: { filter: { _id: users[2]._id, a: 1 }, update: { $set: { name: 'I am the updated third name' } } } }
{ updateOne: { filter: { _id: users[2]._id, a: 3 }, update: { $set: { name: 'I am the updated third name' } } } }
];

assert.deepEqual(
Expand Down

0 comments on commit 693398f

Please sign in to comment.