[英]mongoose plugin doesn't hook on findOneAndUpdate
I setup a mongoose schema like so:我设置了一个 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)
I'm using the mongoose-auto-increment plugin to auto-increment my monogodb document's id's (actually using this fork, but they are basically the same)我正在使用 mongoose -auto-increment插件来自动增加我的 monogodb 文档的 id(实际上是使用这个fork,但它们基本相同)
I have some code that seeds my dev.我有一些代码可以为我的开发者提供种子。 db on restart which looks like this: 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();
This code works great and I end up with a document that looks like:这段代码效果很好,我最终得到了一个看起来像这样的文档:
{ "_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" } ] }
However when my frontend posts this edited data back to me I get json that looks like:但是,当我的前端将此编辑后的数据发回给我时,我得到的 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 } ] }
Now what I want is to update existing embedded documents and/or add new ones if needed.现在我想要的是更新现有的嵌入式文档和/或在需要时添加新的。 I setup this code for this purpose (I appreciate there is probably a much more intelligent way to implement this, but even so, it sort of "works"):我为此目的设置了这段代码(我很欣赏可能有一种更智能的方式来实现它,但即便如此,它还是有点“有效”):
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 }) } })
Unfortunately, is this scenario my plugin doesn't "trigger" or invoke and I end up with a document updated in the collection which lacks a "_id" property for the newly pushed contactMethod.不幸的是,在这种情况下,我的插件是否没有“触发”或调用,我最终在集合中更新了一个文档,该文档缺少新推送的contactMethod的“_id”属性。
Why is my plugin not being called on the findOneAndUpdate call?为什么我的插件没有在 findOneAndUpdate 调用中被调用?
I found this question but my plugin doesn't use ES6 syntax.我发现了这个问题,但我的插件不使用 ES6 语法。 Also I found this comment but the current mongoose documentation doesn't state that anymore so I was kinda hoping maybe they changed/fixed it.我也发现了这个评论,但是当前的 mongoose 文档不再是 state 所以我有点希望他们可能改变/修复它。
Thanks, (first question. phew..)谢谢,(第一个问题。唷..)
I eventually solved (or rather worked around this) by changing my code and splitting up the addition of a new contact method to two operations: findOne and save, like so:我最终通过更改我的代码并将新的联系方法添加到两个操作中来解决(或者更确切地说是解决这个问题):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.