title: Update dynamic field in Mongoose schema
body: <p>I have a simple application that communicates with an API build with NodeJS+Express+MongoDB (mongoose). My question is referred to the API side.</p>
  
  <p>I use this schema to store user configuration in MongoDB:</p>
  
  <pre><code>{
      user: Number,
          name: String,
          lists: {
              invoices: {
                  year: Number,
                  orderCol: String,
                  order: Number
              }
          }  
      }
  }
  </code></pre>
  
  <p>There is only one document per user, that stores values used by my frontend app to show a list of invoices sorted and filtered by year. </p>
  
  <p>I need to update the document, but it has to update only one property each time. For example, with a call to API I want to be able to change <code>user.lists.invoices.year</code> to 2017, without changing <code>order</code>&amp;<code>orderCol</code>.</p>
  
  <p>I'm sending three values to the API: type of list (<code>invoices</code>), property to change (<code>year</code>) and value to set (<code>2017</code>).</p>
  
  <pre><code>// PUT request with this payload
  { 
      list: 'invoices',
      property: 'year',
      value: 2017
  }
  </code></pre>
  
  <p>I'm using <code>user</code> field to query.</p>
  
  <pre><code>router.put('/lists/:usr', (req, res, next) =&gt; {
      var listData = req.body;  // listData.list, listData.property, listData.year
      var usr = req.params.usr;
      Config.findOneAndUpdate(
          { user: usr }, 
          { $set: { ??? } },
          { new: true, upsert: true },
          (err, config) =&gt; {
              if (err) return next(err);
              res.json({ data: config });
          }
      )
  }) 
  </code></pre>
  
  <p>Note for possible dupplicates: my searches in Google and SO always lead me to <code>$</code> operator, but I don't know if it's the solution for this, and in any case I don't know how to use it in this case.</p>
  
  <p>Maybe it's just I'm not designing the right Schema, in which case I'd aprecciate any advice.</p>