繁体   English   中英

mongoose 插件无法挂接到 findOneAndUpdate

[英]mongoose plugin doesn't hook on findOneAndUpdate

我设置了一个 mongoose 架构,如下所示:

 const expertSchema = new mongoose.Schema({ firstName: { type: String, required: true, }, lastName: { type: String, required: true, }, contactMethods: { type: [ContactMethod.schema], } }) expertSchema.plugin(autoIncrement.plugin, { model: 'EXP-', startAt: 1, prefix: 'EXP-' }); const Expert = mongoose.model('Expert', expertSchema); const contactMethodsSchema = new mongoose.Schema({ type: { type: String, required: true, }, value: { type: String, required: true, }, preferred: { type: Boolean } }); contactMethodsSchema.plugin(autoIncrement.plugin, { model: 'CON-', startAt: 1, prefix: 'CON-' }); const ContactMethod = mongoose.model('ContactMethod', contactMethodsSchema)

我正在使用 mongoose -auto-increment插件来自动增加我的 monogodb 文档的 id(实际上是使用这个fork,但它们基本相同)

我有一些代码可以为我的开发者提供种子。 db 在重新启动时看起来像这样:

 const contMethod1 = new models.ContactMethod({ type: 'email', value: "janedoe@acme.org", preferred: false, }); const contMethod2 = new models.ContactMethod({ type: 'phone', value: "+12125550711", preferred: true, }); const expert1 = new models.Expert({ firstName: 'Jane', lastName: 'Doe', contactMethods: [contMethod1, contMethod2] }); expert1.save();

这段代码效果很好,我最终得到了一个看起来像这样的文档:

 { "_id": "EXP-1", "firstName": "Jane", "lastName": "Doe", "contactMethods": [{ "type": "email", "value": "janedoe@acme.org", "preferred": false, "_id": "CON-1" }, { "type": "phone", "value": "+12125550711", "preferred": true, "_id": "CON-2" } ] }

但是,当我的前端将此编辑后的数据发回给我时,我得到的 json 看起来像:

 { _id: "EXP-1", "contactMethods": [{ "type": "email", "value": "janedoe@acme.org", "preferred": false, "_id": "CON-1" }, { "type": "phone", "value": "+12125550711", "preferred": true, "_id": "CON-2" }, { "type": "whatsapp", "value": "+123456789", "preferred": false } ] }

现在我想要的是更新现有的嵌入式文档和/或在需要时添加新的。 我为此目的设置了这段代码(我很欣赏可能有一种更智能的方式来实现它,但即便如此,它还是有点“有效”):

 req.body.contactMethods.map((_, index) => { if (_._id) { expert = req.context.models.Expert.findOneAndUpdate({ "contactMethods._id": _._id }, { $set: { "contactMethods.$.type": _.type, "contactMethods.$.value": _.value, "contactMethods.$.preferred": _.preferred } }, { useFindAndModify: false, returnOriginal: false, runValidators: true }) } else { expert = req.context.models.Expert.findOneAndUpdate({ "_id": req.user._id, }, { $push: { "contactMethods": _ } }, { useFindAndModify: false, returnOriginal: false, runValidators: true }) } })

不幸的是,在这种情况下,我的插件是否没有“触发”或调用,我最终在集合中更新了一个文档,该文档缺少新推送的contactMethod的“_id”属性。

为什么我的插件没有在 findOneAndUpdate 调用中被调用?

我发现了这个问题,但我的插件不使用 ES6 语法。 我也发现了这个评论,但是当前的 mongoose 文档不再是 state 所以我有点希望他们可能改变/修复它。

谢谢,(第一个问题。唷..)

我最终通过更改我的代码并将新的联系方法添加到两个操作中来解决(或者更确切地说是解决这个问题):findOne 和保存,如下所示:

req.body.contactMethods.map((_, index) => {
  if (_._id) {
    expert = req.context.models.Expert.findOneAndUpdate({
      "contactMethods._id": _._id
    }, {
      $set: {
        "contactMethods.$.type": _.type,
        "contactMethods.$.value": _.value,
        "contactMethods.$.preferred": _.preferred
      }
    }, {
      useFindAndModify: false,
      returnOriginal: false,
      runValidators: true
    })
  } else {
    expert = req.context.models.Expert.findOne({
      "_id": req.user._id,
    })
    expert.contactMethods.push(_);
    expert.save({ validateModifiedOnly: true });
  }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM