title: Mongoose, removing an object from an array and updating other objects
body: <p>I'm a web development rookie and I'm trying to build a simple editable image gallery. I have a Collections schema and a Paintings schema:</p>
  
  <pre><code>var collectionSchema = new mongoose.Schema({
      name: String,
      paintings: [
          {
              type: Schema.Types.ObjectId,
              ref: "Painting"
          }
      ]
  })
  </code></pre>
  
  <p>&amp;&amp;</p>
  
  <pre><code>var paintingSchema = new mongoose.Schema({
      img: String,
      description: String,
      index: Number,
      parent: String
  });
  </code></pre>
  
  <p>I'm currently trying to add a destroy route which will delete one painting from one collection. I have this so far:</p>
  
  <pre><code>app.delete("paintings/:type/:index", function(req,res){
      var type = req.params.type;
      var index = req.params.index;
      Painting.findOneAndRemove({parent:type,index:index},function(err,removedPainting){
          if(err){
              console.log(err);
          } else{
              res.redirect("/paintings/"+type);
          }
      });
  });
  </code></pre>
  
  <p>However, I also need all of the "index" attributes of objects with higher indices than that of the deleted object to decrement by one after the object is deleted. Any idea as to what the best way to do this is? </p>
  
  <p>I have tried:</p>
  
  <ol>
  <li><p>changing the destroy route into an update route, and then just completely replacing the array of Paintings within the Collection to an updated version, but that doesn't seem to work--I think because the array within the Collection schema is not actually an array of objects, but an array of ObjectIds?</p></li>
  <li><p>Redirecting (from end of the destroy route) to an update route which loops through the Paintings found by Paintings.find({parent: type}) and increments their ids, but I never actually got to test this because when I tried to redirect from the destroy route I would always get the error: "cannot GET paintings/:type/:index/delete" even though I had made the route a put request, not a get request.</p></li>
  </ol>
  
  <p>Any ideas? Anything would be much appreciated, I'm clearly kind of fumbling around in the dark here. Thanks a ton!</p>