title: Updating sub array in JSON with a REST API in Mean Stack
body: <p>I'm developing a MEAN stack application and I'm hung up on how to actually update a document that has been saved into the MongoDB already. I've seen that I have to use <code>patch</code> instead of <code>post</code> in my REST API paths, but it's still a little clouded to me. I want to insert a new Package into the Package JSON Array in the User JSON. </p>
  
  <p>Possible <a href="https://stackoverflow.com/questions/38507838/how-to-update-a-specific-object-in-a-array-of-objects-in-node-js-and-mongoose">Duplicate</a>, but he's overriding a value in the array and not adding a new object into it. </p>
  
  <p>My JSON Schema: </p>
  
  <pre><code>//User schema
  const UserSchema = mongoose.Schema({
    name: {
      type: String
    },
    email: {
      type: String,
      require: true
    },
    username:{
      type:String,
      required: true
    },
    password:{
      type:String,
      required: true
    },
    packages: [{
      from: String,
      to: String,
      tracking: String
  
    }]
  });
  </code></pre>
  
  <p>My REST API Paths</p>
  
  <pre><code>//Update
  router.patch('/update', (req, res) =&gt; {
    const username = req.body.username;
    const packages = req.body.packages;
  
    User.getUserByUsername(username, (err, user) =&gt; {
      if(!user){
        return res.json({success: false, msg: 'User not found'});
      } else {
        User.addPackages(user, req.body.packages, (err, user) =&gt; {
          if(err){
            res.json({success: false, msg:'Failed to update packages'});
          } else {
            res.json({success: true, msg:'update packages'});
          }
        })
      }
    });
  });
  </code></pre>
  
  <p>My Module's: </p>
  
  <pre><code>module.exports.addPackages = function(user, packages, callback){
    User.findOneAndUpdate(
      {username:user.username},
      {$push: {"packages" : {
        "to" : packages.to,
        "from" : packages.from,
        "tracking" : packages.tracking
      }}},
      {new:true},
      function(err, newPackage){
        if (err) throw err;
      });
  }
  
  module.exports.getUserById = function(id, callback){
    User.findById(id, callback);
  }
  
  module.exports.getUserByUsername = function(username, callback){
    const query = {username: username}
    User.findOne(query, callback);
  }
  </code></pre>
  
  <p>They're updating into my MongoDB, but just the object ID and not the values...</p>