我在 mongoose 的 findOneAndUpdate 中遇到问题。 情况是我正在通过查找来更新文档。 查询如下:
UserModel.findOneAndUpdate({
individualId: 'some id'
}, {
$push: {
supporterOf: 'some string'
}
})
'supporterOf' 是 UserModel 的引用,它的类型是 'ObjectId'。
The issue i am facing here is that, 'some string' is being pushed twice under 'supporterOf' in the document.
谁能告诉我如何将数组元素压入文档中?
最佳答案
我遇到了同样的问题,解决方案是。
我一直在等待,如下所示。
**await** schema.findOneAndUpdate(queryParms, {
"$push": {
"array1": arrayDetails,
"array2": array2Details
}
}, {
"upsert": true,
"new": true
},
function (error, updateResponse) {
if (error) {
throw new Error (error);
} else {
// do something with updateResponse;
}
});
简单地删除 await 帮助我解决了这个问题。 需要找到根本原因。 欢迎任何引用指针。
关于arrays - findOneAndUpdate 导致重复问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55103351/