简体   繁体   English

如何使用nodejs删除mongoose对象数组中的特定元素?

[英]How to remove the specific element in an array of objects in mongoose using nodejs?

I have to update an element in array so I am pulling the value first and then pushing it with new data.我必须更新数组中的一个元素,所以我先拉取值,然后用新数据推送它。 I am trying this code and I am getting error in pull and push syntax.我正在尝试此代码,但在拉和推语法中出现错误。

  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");
                    }
            });
            }
    });

Below is the Mongoose schema structure.下面是猫鼬模式结构。 I want to pull only the element 'status' in Addtasks array and push it with new one.我只想在 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
}

I think you can simplify to one query using $set with the positional operator :我认为您可以使用带有位置运算符的$set简化为一个查询:

User.findOneAndUpdate({email:req.user.email}, 
                      { $set: { "Addtasks.$" : req.query.selectedValue } })

This would update the first matching array element.这将更新第一个匹配的数组元素。 If you want to update all elements use this operator :如果要更新所有元素,请使用此运算符

User.findOneAndUpdate({email:req.user.email}, 
                      { $set: { "Addtasks.$[]" : req.query.selectedValue } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM