![](/img/trans.png)
[英]Mongoose findOneAndUpdate doesn't find (or update) in Node
[英]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.