Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose update method cannot change createdAt field #8619

Closed
aminlatifi opened this issue Feb 25, 2020 · 4 comments · Fixed by #15000
Closed

Mongoose update method cannot change createdAt field #8619

aminlatifi opened this issue Feb 25, 2020 · 4 comments · Fixed by #15000
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@aminlatifi
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Model update method does not change createdAt field!

If the current behavior is a bug, please provide the steps to reproduce.

I want to change createdAt field to newCreatedAt value. I wrote the bellow script and it wasn't useful:

        const result = await Donations.findOneAndUpdate(
           { _id },
           { createdAt: newCreatedAt },
         ).exec();

Instead of findOneAndUpdate, I tried with update method too and result is the same.
However, this script works!

        const [d] = await Donations.find({_id}).exec();
        d.createdAt = newCreatedAt;
        const result = await d.save();

What is the expected behavior?
Update script should be able to update the createdAt field.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose: 5.1.3,
node: v10.9.0
Mongo: 4.2.2

@vkarpov15
Copy link
Collaborator

Set the timestamps option to false for your update, see updateOne() docs. We need to add this option to findOneAndUpdate() docs.

'use strict';
  
const mongoose = require('mongoose');

mongoose.set('debug', true);
mongoose.set('useFindAndModify', false);

const { Schema } = mongoose;

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  await mongoose.connection.dropDatabase();

  const schema = Schema({ name: String }, { timestamps: true });
  const Model = mongoose.model('Test', schema);

  // Sets `createdAt` to June 1, 2015
  await Model.findOneAndUpdate({}, { createdAt: new Date('2015/06/01') }, { timestamps: false });
}

@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Feb 27, 2020
@vkarpov15 vkarpov15 added this to the 5.9.3 milestone Feb 27, 2020
@sean-hill
Copy link

@vkarpov15 this is not working for me. After making the update with the example above, and querying the fresh document, my createdAt timestamp is still the same 😭

@vkarpov15
Copy link
Collaborator

@sean-hill this was for Mongoose 5. In more recent versions of Mongoose, we've made createdAt into an immutable property, so we need a little extra work. The following should work in Mongoose 8:

  // Sets `createdAt` to June 1, 2015
  await Model.findOneAndUpdate({}, { createdAt: new Date('2015/06/01') }, { timestamps: false, strict: false });

However, we should make overwriteImmutable an option so we don't have to disable strict mode to overwrite immutable properties. And add this pattern to timestamps docs

@vkarpov15 vkarpov15 reopened this Nov 1, 2024
@vkarpov15 vkarpov15 modified the milestones: 5.9.3, 8.8.1 Nov 1, 2024
vkarpov15 added a commit that referenced this issue Nov 4, 2024
fix(query): add `overwriteImmutable` option to allow updating immutable properties without disabling strict mode
@sivastu
Copy link

sivastu commented Nov 18, 2024

Its working. @vkarpov15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
4 participants