From c2352f32122f753ac77170b052da9e52d99440bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mads=20Toustrup-L=C3=B8nne?= <matl@priceshape.dk>
Date: Thu, 30 May 2024 09:55:25 +0200
Subject: [PATCH] Make sure buildBulkWriteOperations targets right shard

---
 lib/model.js       | 11 +++++++++++
 test/model.test.js | 10 +++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/lib/model.js b/lib/model.js
index 27af8745207..a62fddf06cb 100644
--- a/lib/model.js
+++ b/lib/model.js
@@ -3956,6 +3956,17 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
 
       _applyCustomWhere(document, where);
 
+      // If shard key is set, add shard keys to _filter_ condition to right shard is targeted 
+      const shardKey = this.schema.options.shardKey;
+      if (shardKey) {
+        const paths = Object.keys(shardKey);
+        const len = paths.length;
+
+        for (let i = 0; i < len; ++i) {
+          where[paths[i]] = shardKey[paths[i]];
+        }
+      }
+
       // Set the discriminator key, so bulk write casting knows which
       // schema to use re: gh-13907
       if (document[discriminatorKey] != null && !(discriminatorKey in where)) {
diff --git a/test/model.test.js b/test/model.test.js
index 74ba4463833..d5937654a45 100644
--- a/test/model.test.js
+++ b/test/model.test.js
@@ -6331,14 +6331,14 @@ describe('Model', function() {
 
       const userSchema = new Schema({
         name: { type: String }
-      });
+      }, { shardKey: { a: 1 } });
 
       const User = db.model('User', userSchema);
 
       const users = [
-        new User({ name: 'Hafez1_gh-9673-1' }),
-        new User({ name: 'Hafez2_gh-9673-1' }),
-        new User({ name: 'I am the third name' })
+        new User({ name: 'Hafez1_gh-9673-1', a: 1 }),
+        new User({ name: 'Hafez2_gh-9673-1', a: 2 }),
+        new User({ name: 'I am the third name', a: 3 })
       ];
 
       await users[2].save();
@@ -6349,7 +6349,7 @@ describe('Model', function() {
       const desiredWriteOperations = [
         { insertOne: { document: users[0] } },
         { insertOne: { document: users[1] } },
-        { updateOne: { filter: { _id: users[2]._id }, update: { $set: { name: 'I am the updated third name' } } } }
+        { updateOne: { filter: { _id: users[2]._id, a: 1 }, update: { $set: { name: 'I am the updated third name' } } } }
       ];
 
       assert.deepEqual(