如何使用nodejs在猫鼬中拉取或删除另一个数组中的数组元素?


Addtasks数组的Mongoose jSON代码格式:
{
"Id4AddtasksBigpaths" : "6cbt51fho5y$s",
"clientName" : "Vikas Yadav",
"deadline" : "Set Deadline",
"assignee" : "Assign",
"displayLock" : "inline",
"displayDelete" : "none",
"commonID" : "s6gf40ca5rq",
"status" : "Requirement Completed",
"Date" : "Thu Sep 17 2020 18:12:23 GMT+0530 (India Standard Time)",
"exampleRadios" : "option1",
"otherdetails" : "trhr",
"website" : "dsfdd.com",
"keywords" : "customer values, revenue targets",
"words" : 33252,
"topic" : "What is a problem that your target customer has?",
"_id" : ObjectId("5f6359af8c5ed924b84fdf71"),
"Bigpaths4Clients" : [],
"Bigpaths" : [ 
{
"path" : "public\files\Best VR Headset.docx",
"name" : "Best VR Headset.docx",
"Id4AddtasksBigpaths" : "6cbt51fho5y$s",
"uniqueId" : "ztojaap0hoqu$id"
}, 
{
"path" : "public\files\chsl_marks2018.pdf",
"name" : "chsl_marks2018.pdf",
"Id4AddtasksBigpaths" : "6cbt51fho5y$s",
"uniqueId" : "gbgz1z404hu$id"
}
]
}

以下是代码:我正在尝试下面的代码来做这件事,但没有工作。我想根据路由中传递的params-id删除Bigpaths数组中的特定对象。我在哪里犯错误?

router.get('/profile/view/removeThumbnail/:id/:uniqueId', function(req, res, next) {
console.log(req.params.id);
console.log(req.params.uniqueId);
User.updateMany({"Addtasks.Id4AddtasksBigpaths":req.params.id}, {$pull: {"Addtasks.$.Bigpaths" : {"Bigpaths.$.uniqueId": req.params.uniqueId}}},
function (error, success) {
if (error) {
console.log(error);
} else {
// res.render('viewTask');
console.log('path removed succesfully');
}
});
})

您的查询是错误的。我帮你修好了。由于在$pull中,您已经在这里的bigpaths数组Addtasks.$.Bigpaths中,您现在只需要指定要删除的搜索数据的键。

User.updateMany({
"Addtasks.Id4AddtasksBigpaths": req.params.id
},
{
$pull: {
"Addtasks.$.Bigpaths": {
"uniqueId": req.params.uniqueId
}
}
})

最新更新