title: how do i find documents whose latest check date is after 2020?
body: <p>i'm new to mongodb. my documents like this, some of the document’s log is empty, and some of the log’s check_date contains empty:</p>
  <pre><code>{ &quot;_id&quot; : ObjectId(&quot;60176a4e771e0000e2007db8&quot;), &quot;file&quot; : 1, &quot;log&quot; : [ { &quot;check_date&quot; : ISODate(&quot;1991-01-01T00:00:00Z&quot;) }, { &quot;check_date&quot; : ISODate(&quot;1992-01-01T00:00:00Z&quot;) }, {  } ] }
  { &quot;_id&quot; : ObjectId(&quot;60176a4e771e0000e2007db9&quot;), &quot;file&quot; : 2, &quot;log&quot; : [ { &quot;check_date&quot; : ISODate(&quot;2019-01-01T00:00:00Z&quot;) }, { &quot;check_date&quot; : ISODate(&quot;2020-01-01T00:00:00Z&quot;) }, { &quot;check_date&quot; : ISODate(&quot;2021-01-01T00:00:00Z&quot;) }, {  } ] }
  { &quot;_id&quot; : ObjectId(&quot;60176a4e771e0000e2007dba&quot;), &quot;file&quot; : 3, &quot;log&quot; : {  } }
  </code></pre>
  <p>how do i find documents whose latest check date is after 2020(file 2)?</p>
  <p>this is my query, but returns 0 files:</p>
  <pre><code>[
    {$match:{log.check_date:{$exists:1}}},
    {$redact:{
      $cond:{
        if:{$gt:[{$max:[$log.check_date]}, &quot;2020-01-01T00:00:00Z&quot;]},
        then:$$KEEP,  
        else:$$PRUNE
        }
     }}
  ]
  </code></pre>
  <p>The reason why empty log is {} instead of [] is because these documents are R's nested data frame. the log of file 3 is not a cell of a data frame with 0 rows, but a cell with NULL.</p>
  <pre><code>&gt; x
  # A tibble: 3 x 2
     file log             
    &lt;int&gt; &lt;list&gt;          
  1     1 &lt;tibble [3 x 1]&gt;
  2     2 &lt;tibble [4 x 1]&gt;
  3     3 &lt;NULL&gt; 
  </code></pre>