Comments (6)
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.
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.
@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.
@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.
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.
This issue was closed because it has been inactive for 19 days and has been marked as stale.
from mongoose.
Related Issues (20)
- empty objects are being saved as null HOT 1
- Autoindex is recreating existing indexes HOT 2
- StrictPopulateError: Cannot populate path `totalLinksList` because it is not in your schema. Set the `strictPopulate` option to false to override. HOT 1
- Breaking types of `toObject` on SubDocument or ArraySubdocument HOT 1
- Make autoIndex syncIndex instead of createIndex
- Using Promise.all inside transactions errors with "Cannot pin multiple connections to the same session" HOT 6
- new Types.ObjectId is deprecated how do i convert a string _id to an ObjectID now? HOT 1
- Filter is mutated in place by Query modifiers
- command events are not fired after setting monitorCommands: true HOT 2
- log : import_mongoose.default.connect is not a function
- Type is incorrect for Query.distinct when field is a nested field HOT 1
- Not Work `new mongoose.Types.ObjectId` HOT 1
- Default values sometimes do not work when creating a document
- Populate does not consider toJSON and id:true HOT 1
- Bulk operations not including shardKey
- MongooseError: Operation `prefixes.findOne()` buffering timed out after 10000ms and MongooseServerSelectionError: connect ECONNREFUSED 13.200.189.28:27017
- MongoServerError: cannot create a new collection -- already using 527 collections of 500
- Is there a way to preserve documents modifications when a doc is modified and then populated?
- Typescript sees the Boolean Constructor Type as Date when using Mongoose
- getting _id as unknown type
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.