title: Add an object to an array with mongoose
body: <p>I have a nodejs webserver that allows file uploads and I would like to add the files uploaded to users, but every time I try to add to the users db array, It says it adds it, but when I read the users files, it returns an empty array. I used the following code to try to add to the array:</p>
  
  <pre><code>for (var x in files){
   User.findByIdAndUpdate(req.user._id,
    {$push: {"files":{name:files[x].filename}}},
    function (err, doc) {
     if (err) throw err;
     console.log(doc);
   });
  }
  </code></pre>
  
  <p>The schema used is this:</p>
  
  <pre><code>var schema = new mongoose.Schema({
   name: {type:String, required: true, trim: true, lowercase:true, unique: true},
   hash: {type:String, required: true },
   files: [],
   premium: {type:Boolean, default: false}
  });
  </code></pre>