This is the 27th day of my participation in the August More Text Challenge

Mongoose Schema has a Timestamps option that tells Mongoose to automatically manage the createdAt and updatedAt properties on the document. For example, here’s how to enable timestamps on the User model.

const userSchema = mongoose.Schema({
  name: String}, {timestamps: true
})
​
const User = mongoose.model('User', userSchema)
const doc = await User.create({ name: 'O.O' })
​
doc.createdAt / / T22:2021-08-20 36:59. 414 z
doc.updatedAt / / T22:2021-08-20 36:59. 414 z
​
doc.createdAt instanceof Date // true
Copy the code

When timestamp is enabled, Mongoose adds createdAt and updatedAt properties to the model. By default, createdAt and updatedAt are of type Date. When you update a document, Mongoose automatically adds updatedAt.

doc.name = 'D.O'
await doc.save()
​
doc.createdAt / / T22:2021-08-20 36:59. 414 z
doc.updatedAt / / T22:2021-08-20 37:09. 071 z
Copy the code

Specific mongoose model writes allow you to skip timestamps if the timestamps are set in the Schema. To do this, you must set timestamps to false and the operation does not update the time.

const userSchema = mongoose.Schema({
  name: String
}, {
  timestamps: true
})
​
const User = mongoose.model('User', userSchema)
​
const doc = await User.findOneAndUpdate({
  email: 'O.O'
}, {
  email: 'D.O'
}, {
  new: true.upsert: true.timestamps: false
})
Copy the code

If you want to block only one of the updates, you should create objects with key-value pairs rather than setting timestamps to false as the value. Depending on the requirements, we just need to set createdAt or updatedAt to true or false as required.

const userSchema = mongoose.Schema({
  name: String
}, { 
  timestamps: true 
})
​
const User = mongoose.model('User', userSchema)
​
const doc = await User.findOneAndUpdate({
  name: 'O.O'
}, {
  name:'D.O'
}, {
  new:true.upsert: true.timestamps: {
    createdAt: false.updatedAt: true}})Copy the code

Alternate attribute name

By default, Mongoose uses createdAt and updatedAt as the timestamp property names. But you can have Mongoose use any property name you like. For example, if you prefer the snake_case attribute name, have Mongoose use created_AT and updated_AT instead:

const userSchema = mongoose.Schema({
  name: String 
}, {
  timestamps: {
    createdAt: 'created_at'.updatedAt: 'updated_at'}})const User = mongoose.model('User', userSchema)
​
const doc = await User.create({
  name: 'O.O'
})
doc.updated_at / / T22:2021-08-20 40:06. 667 z
Copy the code

Use Unix timestamps

While the date type is usually sufficient, you can also have Mongoose store the timestamp as seconds since January 1, 1970. Mongoose Schema supports the timestamps. CurrentTime option, which allows you to pass custom functions to get the currentTime.

const userSchema = mongoose.Schema({
  name: String
}, {
  // Let Mongoose use Unix time (seconds since January 1, 1970)
  timestamps: {
    currentTime: () = > Math.floor(Date.now() / 1000)}})Copy the code