title: Update element of an array using aggregation pipeline in `update` operation
body: <h1>Scenario</h1>
  <p>For the below document, I want to update the <code>status</code> of a specific <strong>message</strong> in the <code>messages</code> array:</p>
  <pre><code>[{
      &quot;_id&quot; : ObjectId(&quot;6079bab4f297df39a44609cb&quot;),
      &quot;title&quot; : &quot;Test messages of single user&quot;,
      &quot;messages&quot; : [ 
          {
              &quot;_id&quot; : ObjectId(&quot;6079bab4f297df39a44609cc&quot;),
              &quot;body&quot; : &quot;Test 1&quot;,
              &quot;status&quot; : 1
          }, 
          {
              &quot;_id&quot; : ObjectId(&quot;6079bab4f297df39a44609cd&quot;),
              &quot;body&quot; : &quot;Test 2&quot;,
              &quot;status&quot; : 1
          }, 
          {
              &quot;_id&quot; : ObjectId(&quot;6079bcf7c041b00ec4cebb9d&quot;),
              &quot;body&quot; : &quot;Hello I'm Sam&quot;,
              &quot;status&quot; : 1
          }
      ]
  }]
  </code></pre>
  <ol>
  <li>Query <strong>without aggregation pipeline</strong> in update operation <strong>updates the status in single message</strong>.</li>
  </ol>
  <p>Run below query <a href="https://mongoplayground.net/p/LWlZGU41rsn" rel="nofollow noreferrer">here</a></p>
  <pre><code>db.update(
    { 
      &quot;_id&quot;: ObjectId(&quot;6079bab4f297df39a44609cb&quot;), 
      &quot;messages&quot;: { $elemMatch: { _id: ObjectId(&quot;6079bcf7c041b00ec4cebb9d&quot;) } }
    }, 
    {
      $set: { &quot;messages.$.status&quot;: 2 }
    }
  )
  </code></pre>
  <ol start="2">
  <li>Query <strong>with aggregation pipeline</strong> in update operation <strong>updates the status in all messages</strong></li>
  </ol>
  <p>Run below query <a href="https://mongoplayground.net/p/FlXDl6Hp4ZE" rel="nofollow noreferrer">here</a></p>
  <pre><code>db.update(
    { 
      &quot;_id&quot;: ObjectId(&quot;6079bab4f297df39a44609cb&quot;), 
      &quot;messages&quot;: { $elemMatch: { _id: ObjectId(&quot;6079bcf7c041b00ec4cebb9d&quot;) } }
    }, 
    [
      {
        $set: { &quot;messages.status&quot;: 2 }
      }
    ]
  )
  </code></pre>
  <p><strong>MongoDB Version: 4.4</strong></p>
  <p><strong>Note:</strong> For some reason, I need to use the second query but I am not able to update the status of single message. It always update all the messages</p>
  <h1>Question</h1>
  <ol>
  <li>Why is the second query updating status in all messages when I've selected the message that I want to update?</li>
  <li>How can I update status of single message with aggregation pipeline (second query)?</li>
  </ol>
  <p>It'll be helpful if you can share link for the query</p>