title: How to update and return all array elements which matches a condition?
body: <p>I tried the below and understood <code>$</code> returns first matching element.</p>
  <pre><code>vardate=newDate();
  date.setDate(date.getDate()-30);
  
  db.getCollection('status').find({
    'data.end_ts': {
      '$lte': date
    },
    $or: [
      {
        &quot;data.risk_status&quot;: 'inactive'
      },
      {
        &quot;data.risk_status&quot;: 'expired'
      }
    ]
  },
  {
    &quot;data.$&quot;: 1
  })
  </code></pre>
  <p>Then I planned to remove <code>projection</code> and do the removal job at java.</p>
  <p>Here, the problem is that I need to remove and insert into another collection. Hence, I can't just use delete.</p>
  <p>I came up with another way so that I can avoid conditions at java.</p>
  <pre><code>db.getCollection('status').aggregate([
    {
      &quot;$match&quot;: {
            $or: [
              {
                &quot;data.risk_status&quot;: 'inactive'
              },
              {
                &quot;data.risk_status&quot;: 'expired'
              }
            ]
          }
    },
    {
      $unwind: &quot;$data&quot;
    },
    {
      $match: {
            'datas.end_ts': {
              '$lte': date
            }
      }
    },
    {
        $group:{
            &quot;_id&quot;:&quot;$_id&quot;,
            &quot;a&quot;:{$push:&quot;$$ROOT&quot;}
        }
    },
    {
        $project:{
            &quot;_id&quot;:1,
            &quot;a.data&quot;:1
        }
    }
  ])
  ])
  </code></pre>
  <p>Is there any other way which deletes and returns the docs. So that I just can save the returned doc to other collection.</p>
  <p>Can I use <code>$out</code> here to do that? I am not sure. Any help which reduces the network round trip time is desirable.</p>