title: Find and Update Object in Mongoose
body: <p>This is my Schema:</p>
  
  <pre><code>const UserSchema = new mongoose.Schema({
    name: String,
    chats: [{
      lastMessage: String,
      lastUpdate: Date
    }]
  });
  </code></pre>
  
  <p>Users Collection:</p>
  
  <pre><code>{
      "_id" : ObjectId("59987ef42aafc45204ee8bc3"),
      "name" : "Nico",
      "chats" : [ 
          {
              "_id" : ObjectId("599e58265cf2799c07925488")
              "lastMessage": "Test",
              "lastUpdate": "Test"
  
          }, 
          {
              "_id" : ObjectId("599e59218d4a52c7071f46df")
              "lastMessage": "Hi",
              "lastUpdate": "01/01/2017"
          }
      ]
  },
  {
      "_id" : ObjectId("59987ef42aafc45204ee8bc3"),
      "name" : "Lucas",
      "chats" : [  
          {
              "_id" : ObjectId("599e59218d4a52c7071f46df")
              "lastMessage": "Hi",
              "lastUpdate": "01/01/2017"
          }
      ]
  }
  </code></pre>
  
  <p>I am trying to implement chat on my app. I am working on my function that will find and update (on each User document, on Chat array with ObjectID as equals being received by request) but i don't know whats the best approach and how to implement it. </p>
  
  <p>The request to my endpoint will give me ChatID, UserID, and MessageBody.
  So my two approaches are:
  1) Find by UserID, and then findAndUpdate on chat array prop where _idChat is equals as ChatID.
  2) Find in Collection where _idChat is equals as ChatID and then update (this should retrieve me Users that have any object with ChatID and then update it)</p>
  
  <p>This is what i am trying to implement (but having nothing being updated):</p>
  
  <pre><code>  static sendMessage(req: express.Request, res: express.Response) {
  
      const message = req.body.message;
  
      const idChat = req.body.idChat;
  
      const userIds = [req.body.sender, req.body.receiver];
  
      const updatePromises = [];
  
      userIds.forEach( userId =&gt; {
        updatePromises.push(
          UserDao['updateChat']
          (
            userId, idChat, 
  {'$set': {'chats.$.lastMessage': message, 'chats.$.lastUpdate': new Date() }},
  
        );
  
      });
  
      Promise.all(updatePromises)
        .then(values =&gt; {
          return res.status(200).json(values);
      }).catch(reason =&gt; {
          return res.status(400).json(reason);
      });
  
  
  userSchema.static('updateChat', (userId, chatId, query) =&gt; {
    return new Promise((resolve, reject) =&gt; {
      if (!_.isObject(query)) {
        return reject(new TypeError('query is not a valid Object.'));
      }
  
      if (!_.isString(userId)) {
        return reject(new TypeError('userId is not a valid String.'));
      }
  
      User
        .update(
          {_id: userId, 'chats._id': chatId},
          {$set: query},
          {upsert: true}
        ).exec((err, updated) =&gt; {
        err ? reject(err) : resolve(updated);
      });
    });
  });
  </code></pre>
  
  <p>Thanks for your help!</p>