title: Node/Mongoose - remove ObjectId from Array
body: <p>I have a page on a website I'm building that has a comments feature. The website is like Yelp for campsites and each campsite in the collection in the mongo db has a field - comments - that stores the id of every comment posted on that campsite and the id refers to an object in another collection entitled comments. Adding, editing, viewing and deleting comments all works apart from when deleting a comment, the id of that comment is not removed from the comments array of its associated campsite.</p>
  
  <p>Here is an example of a record for one campsite currently:</p>
  
  <pre><code>{ 
       "_id" : ObjectId("5b18331953b9811c54ddc4cd"),
            "contact" : { 
            "phone" : "403-748-4066", 
            "email" : "", 
            "website" : "https://www.albertaparks.ca/parks/central/aspen-beach-pp/information-facilities/camping/lakeview/" 
       }, 
       "author" : { 
            "id" : ObjectId("5a998953342cbc7fe8521ef5"), 
            "username" : "Adam" 
       }, 
       "createdAt" : ISODate("2018-06-06T19:04:25.418Z"), 
       "comments" : [ 
            ObjectId("5b1bf59f8f21e152cce41b49") 
       ], 
       "name" : "Lakeview", 
       "cost" : "40", 
       "location" : "Range Rd 284, Bentley, AB T0C 0J0, Canada", 
       "image" : "https://c1.staticflickr.com/9/8620/16612167676_6418f1b470_b.jpg",
        "description" : "Open, pull-through sites are well-suited to RVs. There are unserviced, power, and power/water options. Trees provide some shade from the summer sun. There are many amenities available including a boat launch, playground, sewage disposal, flush toilets and showers. Camp along Gull Lake and enjoy swimming, boating, and the beach. Explore the trails and boardwalks for walking, biking, bird and wildlife watching. ", 
       "lat" : 52.4357744, 
       "__v" : 1, 
       "long" : -113.9873582 
  }
  </code></pre>
  
  <p>As you can see the campsite object currently refers to one comment but if I was to delete the comment via the UI, it would stop showing the comment as it would be deleted from the comments collection but the reference to the comment in the campsite object would remain. Here is my code currently to delete a comment:</p>
  
  <pre><code>app.delete("/campgrounds/:id/comments/:comment_id", middleware.checkCommentOwnership, function(req, res){
      // RESTful - DESTROY
      Comment.findByIdAndRemove(req.params.comment_id, function(err){
          if(err){
              req.flash("error", "Uh Oh! Something went wrong.");
              res.redirect("/campgrounds");
          }
          Campground.findById(req.params.id, function(err, campground){
              if(err){
                  req.flash("error", "Uh Oh! Something went wrong.");
                  res.redirect("/campgrounds");
              }
              campground.comments.remove(req.params.comment_id);
              req.flash("success", "Comment has been deleted!");
              res.redirect("back");
          });
      });
  });
  </code></pre>
  
  <p>However the line</p>
  
  <pre><code>campground.comments.remove(req.params.comment_id); 
  </code></pre>
  
  <p>doesn't seem to work. I have also (crudely) tried</p>
  
  <pre><code>campground.comments.remove("ObjectId(\"" + req.params.comment_id + "\")")
  </code></pre>
  
  <p>to try to mirror the format that the ids are stored in, in the comments array but this threw this error: </p>
  
  <pre><code>CastError: Cast to ObjectId failed for value "ObjectId("5b1bf4215f319752ab28ba49")" at path "comments"
  </code></pre>
  
  <p>I am using Mongoose primarily to interface with the db.</p>
  
  <p>Sorry if this is long winded or a repeat, I tried to research the solution but could find nothing - at least nothing that I could understand!</p>
  
  <p>Thanks.</p>