【问题标题】:Which way of creating and updating a document is better in mongoose.js?在 mongoose.js 中哪种创建和更新文档的方式更好?
【发布时间】:2020-06-03 03:46:08
【问题描述】:

在使用其他 mongoose 方法(例如 findOneAndUpdate())时,哪种保存文档的方法更好,这样它就不会导致任何错误?

const doc = new Doc();
// Some lines of the code
doc.save(...)
.then(...)
.catch(...);

           OR,

const doc = {};
//Some lines of code
Doc(doc).save()
.then(...)
.catch(...)

【问题讨论】:

    标签: node.js express mongoose


    【解决方案1】:

    创建新文档时使用 doc.save()。并在更新现有文档时使用 findOneAndUpdate()。两者互不干扰。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多