Mongo:更新文档并添加/更新lastModified

时间:2020-01-19 23:51:14

标签: mongodb

我需要更新文档匹配项。

用于更新文档的字段存储在对象项中

查询:

bulk.find({ id: item.id }).upsert().updateOne({ $set: item, $currentDate : {lastModified: true} })

{$ set:item}-完成工作并将项目中的所有字段添加到文档中,但是当我添加$ currentDate字段时,mongo返回:

 errmsg: "Updating the path 'lastModified' would create a conflict at 'lastModified'",

我应该只执行item.lastModified =(new Date())。getTime(),还是可以通过查询来做到?

1 个答案:

答案 0 :(得分:0)

这是一个Mongo功能错误。在单个操作中,您不能多次更改单个文件。 $ set-设置变量lastModified,然后$ currentDate尝试立即对其进行修改。解决方法是将lastModified添加到对象项

item.lastModified = new Date()
bulk.find({ id: item.id }).upsert().updateOne({ $set: item })
相关问题