title: Mongoose update an item from array by id
body: <p>In a node js project, where I use mongoose, I'm trying to update an item of array inside a document. <br>
  I have a <code>users</code> schema which has a <code>notifications</code> array inside. Each user has an <code>_id</code> and each notifications item has an <code>_id</code>.</p>
  
  <p>How can update a notification by, user id and notification id?</p>
  
  <p><strong>USER SCHEMA:</strong></p>
  
  <pre><code>const schema = mongoose.Schema({
    notifications: [{
      id: { type: mongoose.Schema.ObjectId },
      title: { type: String },
      body: { type: String },
      isRead: { type: Boolean, default: false },
      date: { type: Date }
    }],
    token: { type: String },
    isActive: { type: Boolean, default: false }
  }, { timestamps: {} }) 
  </code></pre>
  
  <p>This is what I've tried so far:</p>
  
  <pre><code>exports.updateNotification = (req, res) =&gt; {
  
    // Note: in req.params I receive id(for user id) and notificationId.
  
    UserModel.findByIdAndUpdate(req.params.id, { $set: { notifications: req.body } }, { safe: true, upsert: true, new: true })
      .then((result) =&gt; res.status(201).json({
        success: true,
        message: res.__('success.add'),
        user: result
      }))
      .catch(err =&gt; errorHandler.handle(err, res))
  }
  </code></pre>
  
  <p>The method <code>findByIdAndUpdate</code> I've used it to updated the user, but I don't know how to adapt it for updating a notification, by id.</p>