Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Mongoose的upsert操作中设置更新时间戳?_Node.js_Mongodb_Mongoose_Upsert - Fatal编程技术网

Node.js 如何在Mongoose的upsert操作中设置更新时间戳?

Node.js 如何在Mongoose的upsert操作中设置更新时间戳?,node.js,mongodb,mongoose,upsert,Node.js,Mongodb,Mongoose,Upsert,我只想在upsert操作中为更新记录设置更新时间戳(并为新记录插入时间戳) self.model.update({Id: obj.Id}, obj, {upsert: true}, function(err){ if (err) { return; } //... }); 有没有办法处理这件事 我可能会使用pre.save中间件,但有没有办法判断当前操作是插入还是更新 提前谢谢 您可以使用预保存挂钩,可以使用许多标志,如.isNew或.isModif

我只想在upsert操作中为更新记录设置更新时间戳(并为新记录插入时间戳)

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

    //...

});
有没有办法处理这件事

我可能会使用pre.save中间件,但有没有办法判断当前操作是插入还是更新


提前谢谢

您可以使用预保存挂钩,可以使用许多标志,如.isNew或.isModified

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

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

对于创建日期,可以从ObjectId提取时间戳,但不能自动执行。您可以使用upsert上的
$set:{timestamp:Date.now()}
来执行此操作

mongoose bugtracker中有一个已关闭的问题在讨论这个问题,这里:

有点晚了,但希望这能帮助到别人

用于插入

db.test.insert({ts:new Date()})
向上插入

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

这有一个mongoose插件。(mongoose时间戳插件)

下面是CoffeeScript实现——如果需要,可以使用js2coffe.com转换为js

你可以在你所有的模型中使用它

创建一个函数,如下所示

# * 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
#*Mongoose时间戳插件
#*版权所有(c)2012尼古拉斯·彭里
#*原创作品版权(c)2012 Brian Noguchi
#*修改自Fino 2014
#*麻省理工学院特许
#
时间戳=(架构,选项)->
updated_at=“updated_at”
created_at=“created_at”
UpdateDataType=日期
createdAtType=日期
如果选项的类型为“对象”
如果typeof options.updated_at为“字符串”
更新的位置=选项。更新的位置
否则,如果typeof options.updated_at为“object”
updated_at=options.updated_at.name或updated_at
updatedAtType=options.updated\u at.type或updatedAtType
如果typeof options.created_at为“字符串”
created_at=options.created_at
否则,如果typeof options.created_at为“object”
created_at=options.created_at.name或created_at
createdAtType=options.created_at.type或createdAtType
dataObj={}
dataObj[updated_at]=updatedAtType
if schema.path(“创建时”)
schema.adddataobj
schema.virtual(创建于).get->
如果此[“”+创建于]
返回此[“\uu+created\u at]=@\u id.getTimestamp()
schema.pre“保存”(下一步)->
如果@isNew
this[updated_at]=this[created_at]
其他的
此[更新日期]=新日期
下一个()
返回
其他的
dataObj[created_at]=createdAtType
schema.adddataobj
schema.pre“保存”(下一步)->
除非此[创建于]
此[创建日期]=此[更新日期]=新日期
其他的
此[更新日期]=新日期
下一个()
返回
返回
然后在猫鼬模型中执行以下操作

userSchema.plugin时间戳

就这样


当您继续使用“model.save()”

时,这将添加created_at并保持update_at更新。如果您关心性能,那么没有办法,因为本机mongodb驱动程序(和DB)不支持它。$update:)的运算符集有限。您现在如何分配时间戳?这不是猫鼬阿法克的固有特征。看起来您可能正在设置整个文档?2.4将为Upsert添加一个新的更新运算符,该运算符在您的案例中非常有用,名为$setOnInsert。您是否能够解决您的问题?。选择正确的答案或张贴您自己的感谢。更新方法似乎无法与预挂钩挂钩。预挂钩仅用于保存。