Skip to content

Timestamp createdAt can't be updated using bulkWrite #15781

@gaastonsr

Description

@gaastonsr

Prerequisites

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

Mongoose version

7.x.x, 8.x.x

Node.js version

20.x

MongoDB server version

8.x

Typescript version (if applicable)

No response

Description

I'm trying to update the createdAt field of some documents using bulkWrite and it's not possible. It works if I use updateOne directly as a function but if I do it through a bulkWrite it doesn't work.

This only affects createdAt, updatedAt can be updated.

So far I have tested it using Mongoose v7 and v8 and both have the same issue, I would appreciate it if this could be fixed in both versions 🙇🏽‍♂️.

Steps to Reproduce

const personSchema = new mongoose.Schema(
  {
    name: { type: String, required: true },
  },
  { timestamps: true }
);

const PersonModel = mongoose.model("Person", personSchema);

const person = new PersonModel({
  name: "John",
});
await person.save();

// With `bulkWrite` it doesn't work
await PersonModel.bulkWrite([
  {
    updateOne: {
      filter: { name: "John" },
      update: {
        createdAt: new Date(0),
      },
      options: {
        timestamps: false,
        overwriteImmutable: true,
      },
    },
  },
]);

// Using `updateOne` directly it works
// await PersonModel.updateOne(
//   { name: "John" },
//   { createdAt: new Date(0) },
//   { timestamps: false, overwriteImmutable: true }
// );

Expected Behavior

I expect to be able to override the createdAt field if needed.

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