title: MongoDB: How to count a field if it's value matches a condition?
body: <p>I have a document that includes a field like this:</p>
  
  <pre><code>{
   ...
   log: [
      {
        utc_timestamp: ISODate("2014-11-15T10:26:47.337Z"),
        type: "clicked"
      },
      {
        utc_timestamp: ISODate("2014-10-15T16:12:51.959Z"),
        type: "emailed"
      },
      {
        utc_timestamp: ISODate("2014-10-15T16:10:51.959Z"),
        type: "clicked"
      },
      {
        utc_timestamp: ISODate("2014-09-15T04:59:19.431Z"),
        type: "emailed"
      },
      {
        utc_timestamp: ISODate("2014-09-15T04:58:19.431Z"),
        type: "clicked"
      },
    ],
    ...
  }
  </code></pre>
  
  <p>How do I get the count of log entries of type "clicked" from this month, only if there is not a log entry of type "emailed" this month?</p>
  
  <p>In other words, I want to find out which clicks have not been sent a related email.</p>
  
  <p>So, in this example, the count would be 1 since the most recent "clicked" entry doesn't have an "emailed" entry.</p>
  
  <p>Note: For this use case, clicks don't have unique IDs - this is all the data that is logged.</p>