title: Count a subdocument property in a projection using MongoDB and Javascript
body: <p>I've this document that represents a room with subRooms, and each subroom has notifications.</p>
  <pre><code> [
        {
          _id: 1,
          id: 1,
          room: &quot;Room1&quot;,
          subRoom: [
            {
              id: &quot;SubRoom1&quot;,
              notifications: [
                {
                  id: 1,
                  read: []
                },
                {
                  id: 2,
                  read: []
                },
                {
                  id: 3,
                  read: []
                },
                {
                  id: 4,
                  read: []
                },
                {
                  id: 5,
                  read: []
                }
              ]
            },
            {
              id: &quot;SubRoom2&quot;,
              notifications: [
                {
                  id: 1,
                  read: [
                    &quot;User1&quot;
                  ]
                },
                {
                  id: 2,
                  read: [
                    &quot;User1&quot;
                  ]
                },
                {
                  id: 3,
                  read: []
                },
                {
                  id: 4,
                  read: []
                },
                {
                  id: 5,
                  read: []
                }
              ]
            }
          ]
        }
      ]
  </code></pre>
  <p>Desired result: (I want to get the notification count if userId is not in notification.read, of every subroom)</p>
  <pre><code>[
    {
      &quot;notRead&quot;: 3,
      &quot;room&quot;: &quot;Room1&quot;,
      &quot;subRoom&quot;: [
        {
          &quot;id&quot;: &quot;SubRoom1&quot;,
          &quot;notRead&quot;: 5
        },
        {
          &quot;id&quot;: &quot;SubRoom2&quot;,
          &quot;notRead&quot;: 3
        }
      ]
    }
  ]
  </code></pre>
  <p>Playground with current code:
  <a href="https://mongoplayground.net/p/uJH9A6B-FQ7" rel="nofollow noreferrer">https://mongoplayground.net/p/uJH9A6B-FQ7</a></p>
  <p>Thank you very much in advance.</p>