Giter Club home page Giter Club logo

Comments (6)

eric-titan avatar eric-titan commented on June 2, 2024

Everything seems good in the _setTimestampsOnUpdate pre middleware, if I console.log(this.getUpdate()) before the next() I see:

{ '$set': { createdAt: 2023-01-10T16:45:36.366Z } }

However, with debug enabled, I can see Mongoose only runs Mongoose: people.findOne({ name: 'John' }, { new: true }) - the createdAt field is being stripped at some point and Mongoose apparently determines the update to be empty.

Similarly, setting {timestamps: {createdAt: false}} correctly yields this in the timestamps middleware:

{
    '$set': {
      createdAt: 2023-01-10T16:49:29.791Z,
      updatedAt: 2024-01-10T16:49:29.792Z
    }
  }

But Mongoose is again stripping out the createdAt field when running the query:

Mongoose: people.findOneAndUpdate({ name: 'John' }, { '$set': { updatedAt: new Date("Wed, 10 Jan 2024 16:49:29 GMT") } }, { returnDocument: 'after', returnOriginal: false })

from mongoose.

eric-titan avatar eric-titan commented on June 2, 2024

Never mind, I missed this update in the 6.x migration guide:

Immutable createdAt
If you set timestamps: true, Mongoose will now make the createdAt property immutable. See gh-10139

Would a PR making this more explicit in the docs be helpful @vkarpov15?

from mongoose.

vkarpov15 avatar vkarpov15 commented on June 2, 2024

@eric-titan yes please, put in a PR with where you think would be a good place to put this information in the docs.

from mongoose.

vkarpov15 avatar vkarpov15 commented on June 2, 2024

@eric-titan I took a closer look and our timestamps docs do have an example of how to overwrite createdAt using findOneAndUpdate() in this section: https://mongoosejs.com/docs/timestamps.html#disabling-timestamps. Below is the code sample:

let doc = await User.create({ name: 'test' });

// To update `updatedAt`, do a `findOneAndUpdate()` with `timestamps: false` and
// `updatedAt` set to the value you want
doc = await User.findOneAndUpdate({ _id: doc._id }, { updatedAt: new Date(0) }, {
  new: true,
  timestamps: false
});
console.log(doc.updatedAt); // 1970-01-01T00:00:00.000Z

// To update `createdAt`, you also need to set `strict: false` because `createdAt`
// is immutable
doc = await User.findOneAndUpdate({ _id: doc._id }, { createdAt: new Date(0) }, {
  new: true,
  timestamps: false,
  strict: false
});
console.log(doc.createdAt); // 1970-01-01T00:00:00.000Z

Is there somewhere else in the docs you would expect to see this example?

from mongoose.

github-actions avatar github-actions commented on June 2, 2024

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

from mongoose.

github-actions avatar github-actions commented on June 2, 2024

This issue was closed because it has been inactive for 19 days and has been marked as stale.

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.