title: node mongoose updating an Object in Array doesn't work
body: <p>I am trying to update an Array of objects wo any success..
  the update statement does not work ..</p>
  
  <p>I may have to update the table instance array and update it with the new valueand then save the table...</p>
  
  <pre><code>  const picked = table.meta.permissions.find(obj =&gt; obj._id == req.params.permissionId);
    console.log('picked: %j', picked); // need to update it !
  </code></pre>
  
  <p>// how can I update this permission object with the new value from req.body ?</p>
  
  <pre><code>  table.save()
      .then(savedTable =&gt; res.json(savedTable))
      .catch(e =&gt; next(e););
  </code></pre>
  
  <p>I have an Array of permissions in a 'meta' field:
  <strong>MODEL</strong></p>
  
  <pre><code>    const Schema = mongoose.Schema;
      const Permission = new Schema({
        permission: {
          role_id: { type: String },
          canWrite: { type: Boolean }
        }
      });
      const TableSchema = new mongoose.Schema({
        meta: {
          name: { type: String, required: true },
          permissions: [Permission],
          ...
        },
        ...
      );
  </code></pre>
  
  <p>In the controller , I firstly load the requested table and append it to the req object, then I execute the updatePermission function, and try to update the table instance permission with new values using $set</p>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>    import Table from '../../models/table.model';
  
      /**
       * Load table  and append to req.
       */
      function load(req, res, next, id) {
        Table.get(id)
          .then((table) =&gt; {
            req.table = table;
            return next();
          })
          .catch(e =&gt; next(e));
      }
  
      function updatePermission(req, res, next) {
        const table = req.table;
  
        console.log('Current table.meta.permissions: %j', table.meta.permissions, '\n');
        console.log('update permission: ', req.params.permissionId, ' with: ', req.body, '\n');
  
        const query = { 'meta.permissions._id': req.params.permissionId }
        const update = { $set: { 'meta.permissions.$.permission': req.body } };
        const options = { new: true};
        table.update(query, update, options)
          .then(savedTable =&gt; res.json(savedTable))
          .catch((e) =&gt; { next(e); });
  </code></pre>
  
  <p>}</p>
  
  <p>The console log displays the current  table permissions and the req.params and req.body </p>
  
  <p>Why the update statement doesn't run correctly </p>
  
  <p>thanks for feedback</p>