title: Mongoose update subarray item
body: <p>I'm trying to update sub-array item in my collection , i'm trying to use set but can't get it work with <code>_id</code> , it only work when I say array[0] ...</p>
  <p>Here is my method :</p>
  <pre><code>    exports.updateSubCategory = (req, res) =&gt; {
        const category = req.category;
        Category.findById(category._id, function (err, doc) {
          if (err) {
           
              return res.status(400).json({
                error: &quot;Can't Find parent category&quot;,
              });
            
          } else {
             doc.subcategory.set(0, { name: req.body.name }); works
             doc.subcategory.set(req.body.id, { name: req.body.name });//doesn't work
             doc.subcategory.set({_id:req.body.id}, { name: req.body.name });//doesn't work
            doc.save((err, updatedCategory) =&gt; {
              if (err) {
                return res.status(400).json({
                  error: &quot;Can't update  subcategory&quot;,
                });
              }
              res.json(updatedCategory);
            });
          }
        });
      };
  </code></pre>
  <p>My schema :</p>
  <pre><code>const mongoose = require(&quot;mongoose&quot;);
  
  const categorySchema = new mongoose.Schema(
    {
      name: {
        type: String,
        trim: true,
        required: true,
        maxlength: 32,
        unique: true,
      },
      subcategory: [
        {
          name: {
            type: String,
            trim: true,
            required: true,
            maxlength: 32,
            unique: true,
          },
        },
      ],
    },
    { timestamps: true }
  );
  
  module.exports = mongoose.model(&quot;Category&quot;, categorySchema);
  </code></pre>