title: Mongoose - search a document and update some fields of a subdocument
body: <p>I'm quite new with Mongoose, I'm trying to get familiar with it,but the poor documentation doesn't help (no search, no list of functions,..).</p>
  
  <p>I have a document that represents a Company, and subdocuments that represents the users:</p>
  
  <pre><code>{
      "_id": "57ffa47f5b70f90831212348",
      "name": "mycompany",
      "address": "...",
      "phone": "...",
      "users": [
          {
              "_id": "57ffa47f5b70f90831212347",
              "username": "alpha",
              "name": "myname",
              "surname": "mysurname",
              "password": "..."
          }
      ]
  }
  </code></pre>
  
  <p>I want to find a particular subdocument and update just some fields (name,surname, password,...) that are passed from frontend.</p>
  
  <p>I receive <code>req.body</code> that contains the fields edited (i.e. req.body.name) and <code>req.user</code> that contains the user logged:</p>
  
  <pre><code>"company_shortname": "CMPNY",
      "company_id": "57ffa47f5b70f90831212348",
      "user": {
          "id_access_level": 0,
          "surname": "ltd",
          "name": "mycompany",
          "username": "mycompanyusername",
          "_id": "57ffa47f5b70f90831212347",
          "password": "..."
      }
  }
  </code></pre>
  
  <p>what I want to obtain is find the subdoc edited (the company that contains this subdoc will be always the same of the user logged in), update data passed, and save.</p>
  
  <p>Using this <a href="https://stackoverflow.com/questions/15874750/partial-update-of-a-subdocument-with-nodejs-mongoose">answer</a>, I tried:</p>
  
  <pre><code>Company.find({'_id': req.user.company_id}).select('name').exec(
          function(err, company) {
  
            if (err) res.status(500).send(err);
  
            var partialUpdate = req.body;
  
            var set = {};
            for (var field in partialUpdate) {
            set['users.$.' + field] = partialUpdate[field];
            }
  
            company.update({_id: req.user._id, "users._id": req.body._id},
            {$set: set},
            function(err, numAffected) {
              console.log('Number of users edited: ' +JSON.stringify(numAffected,null,4));
            });
        });
  </code></pre>
  
  <p>but I cannot get it to work.. any ideas?</p>