29

I have the following message schema in mongoose:

var messageSchema = mongoose.Schema({
  userID: { type: ObjectId, required: true, ref: 'User' },
  text:   { type: String, required: true }
},
{
  timestamps: true
});

Is there anyway to ignore the updatedAt timestamp? Messages won't be updated so updatedAt will be wasted space

Juan Fuentes
  • 1,743
  • 2
  • 20
  • 33

4 Answers4

74

Maybe even better with Mongoose v5 is to do the following;

const schema = new Schema({
  // Your schema...
}, {
  timestamps: { createdAt: true, updatedAt: false }
})
robinvdvleuten
  • 1,462
  • 1
  • 16
  • 18
29

Edit I've amended the answer to reflect the better option to use the default as per @JohnnyHK

You can handle this yourself by declaring the createdAt (or whatever you want to call it) in your schema:

mongoose.Schema({
  created: { type: Date, default: Date.now }
  ...

Alternatively we can also update values on new document in a pre save hook:

messageSchema.pre('save', function (next) {
  if (!this.created) this.created = new Date;
  next();
})

Along those lines is also the flag isNew which you can use to check if a document is new.

messageSchema.pre('save', function (next) {
  if (this.isNew) this.created = new Date;
  next();
})
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • 3
    You can skip the middleware if you define a default: `created: { type: Date, default: Date.now }` – JohnnyHK Aug 11 '16 at 22:46
  • Yes of course. My bad. I've had weird unwanted effects from defaults that I explicitly write it all out now but in this case it's much better. – cyberwombat Aug 11 '16 at 23:36
1

Older topic but there may be a better option depending on your schema... If you're sticking with the default of having mongodb/mongoose auto-gen _id, there's already a timestamp built in. If all you need is "created" and not "updated" just use...

document._id.getTimestamp();

From MongoDB docs here... ObjectId.getTimestamp()

And Here... stackoverflow

docb45
  • 274
  • 3
  • 9
1

Mongoose timestamp interface has these optional fields.

interface SchemaTimestampsConfig {
    createdAt?: boolean | string;
    updatedAt?: boolean | string;
    currentTime?: () => (Date | number);
  }

We can pass the boolean for the field we want(createdAt: true and updatedAt: true will add both fields). We can use the currentTime function to overwrite the date format.

example:

import mongoose from 'mongoose';

const { Schema } = mongoose;
const annotationType = ['NOTES', 'COMMENTS'];
const referenceType = ['TASKS', 'NOTES'];
const AnnotationSchema = new Schema(
  {
    sellerOrgId: {
      type: String,
      required: true,
    },
    createdById: {
      type: String,
      required: true,
    },
    annotationType: {
      type: String,
      enum: annotationType,
    },
    reference: {
      id: { type: String, index: true },
      type: {
        type: String,
        enum: referenceType,
      },
    },
    data: Schema.Types.Mixed,
  },
  { timestamps: { createdAt: true },
);
const AnnotationModel = mongoose.models.annotation || mongoose.model('annotation', AnnotationSchema);
export { AnnotationModel, AnnotationSchema };

Jha Nitesh
  • 188
  • 1
  • 11