Answers for "delete one mongoose"

4

mongoose find by and delete

Campground.findByIdAndRemove(req.params.id, function(err){
      if(err){
          res.redirect("/campgrounds");
      } else {
          res.redirect("/campgrounds");
      }
   });
Posted by: Guest on August-10-2020
0

delete document mongoose

Tank.deleteOne({ size: 'large' }, function (err) {
  if (err) return handleError(err);
  // deleted at most one tank document
});
Posted by: Guest on December-12-2019
0

how to delete popultate subdocument mongoose

exports.feed = function (req, res) {
  var userIds = req.user.friends;

  Surf
    .find({user_id: {$in: userIds}})
    .populate('user_id', 'name boards') // added boards
    .populate('friends', 'name')
    // .populate('board_id', 'name size') // can't do this as discussed
    .exec(function (err, surfs) {
      if (err) {
        return handleError(res, err);
      }

      surfs.forEach(function (surf) {
        surf.boardInfo = surf.user_id.boards.id(surf.board_id)
      });
      // TODO: now remove the surf.user_id.boards.
      return res.json(200, surfs);
    });

};
Posted by: Guest on July-26-2020
0

mongoose remove

const res = await Character.remove({ name: 'Eddard Stark' });
res.deletedCount; // Number of documents removed
Posted by: Guest on June-09-2021

Browse Popular Code Answers by Language