Skip to content

bulkWrite operations on any sharded collection are completely broken in 8.4.1+ #14753

Closed
@adf0nt3s

Description

@adf0nt3s

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.3.4

Node.js version

18.20.4

MongoDB server version

6.x

Typescript version (if applicable)

5.4.5

Description

buildBulkWriteOperations adds the shard key to the filter condition, but it sets it to the schema's value, not the document's value. The test that covers this bit of logic passed because it wasn't really using an updated shard key value

Steps to Reproduce

I've opened a PR here. The test i adjusted will fail with the current logic

For convenience, i've pasted the test below

    it('builds write operations', async() => {

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

      const User = db.model('User', userSchema);

      const users = [
        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();
      users[2].name = 'I am the updated third name';

      const writeOperations = User.buildBulkWriteOperations(users);

      const desiredWriteOperations = [
        { insertOne: { document: users[0] } },
        { insertOne: { document: users[1] } },
        { updateOne: { filter: { _id: users[2]._id, a: 3 }, update: { $set: { name: 'I am the updated third name' } } } }
      ];

      assert.deepEqual(
        writeOperations,
        desiredWriteOperations
      );

    });

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions