How to set update timestamp in upsert operation in Mongoose?

I want to set the update timestamp only for updating the record (and insert timestamp for new record) in the upsert operation.

self.model.update({Id: obj.Id}, obj, {upsert: true}, function(err){
    if (err) {
        return;
    }

    //...

});

Is there anyway to handle this?

There is a pre.save middleware I could use maybe, but is there any way tell whether current operation is insert or update?

Thanks in advance!

you could use a pre save hook, there are a number of flags like .isNew or .isModified you can use.

self.schema.pre('save', function (next) {

  if (this.isNew) {
    this.updated = Date.now();
  }
  return next();
}

For the creation date a timestamp can be extracted from the ObjectId

You can't do it automatically. You can do it using $set: { timestamp: Date.now() } on upsert.

There is a closed issue in mongoose bugtracker discussing this, here: defaults are ignored on upsert

A little late, but hope that helps someone!

for insert

db.test.insert({ts:new Date()})

for upsert

db.test.update({"_id" : ObjectId("540815fa802a63ddca867bc0")},{$set:{ts:new Date()}},{upsert:1})

There is a mongoose plugin for this.( Mongoose Timestamps Plugin )

Here is the CoffeeScript implementation - use js2coffe.com to convert to js if u need.

U can use this in all your Models.

Create a function as below

# * Mongoose Timestamps Plugin
# * Copyright(c) 2012 Nicholas Penree <nick@penree.com>
# * Original work Copyright(c) 2012 Brian Noguchi
# * Modified from Fino 2014
# * MIT Licensed
#
timestamps = (schema, options) ->
  updated_at = "updated_at"
  created_at = "created_at"
  updatedAtType = Date
  createdAtType = Date
  if typeof options is "object"
    if typeof options.updated_at is "string"
      updated_at = options.updated_at
    else if typeof options.updated_at is "object"
      updated_at = options.updated_at.name or updated_at
      updatedAtType = options.updated_at.type or updatedAtType
    if typeof options.created_at is "string"
      created_at = options.created_at
    else if typeof options.created_at is "object"
      created_at = options.created_at.name or created_at
      createdAtType = options.created_at.type or createdAtType
  dataObj = {}
  dataObj[updated_at] = updatedAtType
  if schema.path("created_at")
    schema.add dataObj
    schema.virtual(created_at).get ->
      this["_" + created_at]  if this["_" + created_at]
      return this["_" + created_at] = @_id.getTimestamp()

    schema.pre "save", (next) ->
      if @isNew
        this[updated_at] = this[created_at]
      else
        this[updated_at] = new Date
      next()
      return

  else
    dataObj[created_at] = createdAtType
    schema.add dataObj
    schema.pre "save", (next) ->
      unless this[created_at]
        this[created_at] = this[updated_at] = new Date
      else
        this[updated_at] = new Date
      next()
      return

  return

Then in your Mongoose Model do the following

userSchema.plugin timestamps

that's it!

This will add created_at and keep update_at updated as you continue to use "model.save()"