[英]node.js - nested array in mongoose findOneandUpdate with arrayFilter
I know this is a duplicate question.我知道这是一个重复的问题。 But I have hit the same road block a lot of other seem to hit in updating nested arrays.
但是我在更新嵌套的 arrays 时遇到了很多其他问题。 I am able to manually code an index value, but obviously thats not useful in practical api deployment.
我可以手动编写索引值,但显然这在实际的 api 部署中没有用。 So can arrayFilters be used on an array with a length of 1?
那么arrayFilters可以用在长度为1的数组上吗? should i restructure the prospect model, and revert this to a subdocument?
我应该重组前景 model,并将其还原为子文档吗? Any help would be great.
任何帮助都会很棒。
the console Error控制台错误
MongoError: No array filter found for identifier 'elem' in path 'resoStatus.representation.$[elem].document' MongoError:在路径“resoStatus.representation.$[elem].document”中找不到标识符“elem”的数组过滤器
The document in mongo mongo中的文档
:
Object_id:5f15a5fe911928412c858fb0
name:"State POA"
postedDate:2020-07-19T05:56:19.738+00:00
assigned:5efd0c3d75bb7122943e3a49
req.params.id: 5f15a5fe911928412c858fb0 req.params.id: 5f15a5fe911928412c858fb0
the function that doesnt work不起作用的 function
router.put(
"/:id/resoStatus/representation/:id",
upload,
auth,
async (req, res) => {
console.log(req.params.id);
const prospect = await Prospect.findOneAndUpdate(
{ "_id": req.body.prospectId },
{
"$set": {
"resoStatus.representation.$[elem].document": req.file.filename,
"resoStatus.representation.$[elem].updatedDate": req.file.uploadDate,
"resoStatus.representation.$[elem].id": req.body.id,
},
upsert: true,
arrayFilters: [{ "elem._id": ObjectID(req.params.id) }],
},
(err) => {
if (err) res.status(400).json(err);
}
);
console.log(prospect.resoStatus.representation);
res.status(200).json(prospect);
}
);
the function that does work确实有效的 function
router.put(
"/:id/resoStatus/representation/:id",
upload,
auth,
async (req, res) => {
console.log(req.params.id);
const prospect = await Prospect.findOneAndUpdate(
{ "_id": req.body.prospectId },
{
"$set": {
"resoStatus.representation.0.document": req.file.filename,
"resoStatus.representation.0.updatedDate": req.file.uploadDate,
"resoStatus.representation.0.id": req.body.id,
},
upsert: true,
arrayFilters: [{ "elem._id": ObjectID(req.params.id) }],
},
(err) => {
if (err) res.status(400).json(err);
}
);
console.log(prospect.resoStatus.representation);
res.status(200).json(prospect);
}
);
the mongoose model mongoose model
representation: [
{
document: String,
name: String,
postedDate: Date,
id: String,
updatedDate: Date,
endpoint: String,
assigned: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
}]
You can use the $
- operator to update the first element that matches the query document:您可以使用
$
- 运算符更新与查询文档匹配的第一个元素:
"$set": {
"resoStatus.representation.$.document": req.file.filename,
"resoStatus.representation.$.updatedDate": req.file.uploadDate,
"resoStatus.representation.$.id": req.body.id,
}
If you need to update all array elements of the matched document, you can use the all-positional operator :如果需要更新匹配文档的所有数组元素,可以使用全位置运算符:
"$set": {
"resoStatus.representation.$[].document": req.file.filename,
"resoStatus.representation.$[].updatedDate": req.file.uploadDate,
"resoStatus.representation.$[].id": req.body.id,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.