Skip to content

Commit 693398f

Browse files
committed
Correct shardkey access in buildBulkWriteOps
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.
1 parent 93ebbe1 commit 693398f

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

lib/model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3631,7 +3631,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
36313631
const len = paths.length;
36323632

36333633
for (let i = 0; i < len; ++i) {
3634-
where[paths[i]] = shardKey[paths[i]];
3634+
where[paths[i]] = document[paths[i]];
36353635
}
36363636
}
36373637

test/model.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6365,9 +6365,9 @@ describe('Model', function() {
63656365
describe('buildBulkWriteOperations() (gh-9673)', () => {
63666366
it('builds write operations', async() => {
63676367

6368-
63696368
const userSchema = new Schema({
6370-
name: { type: String }
6369+
name: { type: String },
6370+
a: { type: Number }
63716371
}, { shardKey: { a: 1 } });
63726372

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

63926392
assert.deepEqual(

0 commit comments

Comments
 (0)