Home > Software design >  How to delete nested object from nested object by name
How to delete nested object from nested object by name

Time:12-04

I have mongodb collection like that enter image description here

I want to delete object with its Team-name and player-name in mongoose . This is the how I tried.

router.post("/deletePlayer", auth, (req, res) => {
  const playerName = req.body.playerName; //'ahmet '
  const teamName = req.body.teamName;//'Team A'
  Item.findOneAndRemove(
    { name: { $players.name: playerName } },
    function (err, docs) {
      if (err) {
        console.log(err);
      } else {
        console.log("Removed User : ", docs);
      }
    }
  );
});

CodePudding user response:

use update with $pull

db.collection.update({
  name: "Team A"
},
{
  $pull: {
    player: {
      name: "c"
    }
  }
})

mongoplayground

  • Related