[英]Get an element from an array of objects with an id in mongodb using nodejs and mongoose
[英]How to remove the specific element in an array of objects in mongoose using nodejs?
我必須更新數組中的一個元素,所以我先拉取值,然后用新數據推送它。 我正在嘗試此代碼,但在拉和推語法中出現錯誤。
User.findOneAndUpdate({email:req.user.email}, {$pull: {Addtasks.status : {commonID:req.query.commonIDs}}},
function (error, success) {
if (error) {
console.log(error);
} else {
User.findOneAndUpdate({email:req.user.email}, {$push: {Addtasks.status: req.query.selectedValue}},
function (error, success) {
if (error) {
console.log("Something wrong when updating data!");
} else {
res.redirect('/taskswriter');
console.log("success");
}
});
}
});
下面是貓鼬模式結構。 我只想在 Addtasks 數組中提取元素“狀態”並用新元素推送它。
{
"_id" : ObjectId("5f4217154a5a411a9473d64b"),
"email" : "charlotte@grumpytext.com",
"name" : "Charlotte Miles",
"Addtasks" : [
{
"commonID" : "66k4xorn77x",
"status" : "Requirement Completed",
"Date" : "Sun Aug 23 2020 12:43:57 GMT+0530 (India Standard Time)",
"exampleRadios" : "option1",
"otherdetails" : "bnbn",
"website" : "asad.com",
"keywords" : "anxiety disorders for children, anxiety disorders for adults",
"words" : 12345,
"topic" : "How article is generated?",
"_id" : ObjectId("5f4217354a5a411a9473d64d")
},
{
"commonID" : "offo357aak",
"status" : "Requirement Completed",
"Date" : "Sun Aug 23 2020 12:44:20 GMT+0530 (India Standard Time)",
"exampleRadios" : "option1",
"otherdetails" : "trhr",
"website" : "dsfdd.com",
"keywords" : "Kali from Kaliyug, Kaliki Vishnu Avatar, Vishnu's 10th Avatar",
"words" : 5678,
"topic" : "When the Kaliyug era will end ?",
"_id" : ObjectId("5f42174c4a5a411a9473d651")
}
],
"__v" : 0
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.