title: How to group by date with interval in Mongo
body: <p>I have the following collection:</p>
  <pre><code>{
      _id: ObjectId(&quot;6192513e0fd6ec5ad80376a7&quot;),
      user: &quot;mark.1239&quot;,
      connection: ISODate(&quot;2021-11-09T23:55:40.342Z&quot;),
      disconnection: ISODate(&quot;2021-11-10T01:10:40.342Z&quot;)
  },
  {
      _id: ObjectId(&quot;6192513e0fd6ec5ad80376a9&quot;),
      user: &quot;john.9874&quot;,
      connection: ISODate(&quot;2021-11-02T07:15:42.318Z&quot;),
      disconnection: ISODate(&quot;2021-11-02T08:40:42.318Z&quot;)
  },
  ...
  </code></pre>
  <p>I want to get each connected users by 15 minutes interval.
  So in this case, John is connected at 7:30 but also at 8:15.</p>
  <p>I can group by connection or disconnection with the 15 minutes interval, but it won't take users connected with connection and disconnection fields outside the 15 minutes interval.</p>
  <p>I actually have this but can't find how to modify it to achieve what I want</p>
  <pre><code>collection.aggregate([
     { 
        &quot;$group&quot;: {
             &quot;_id&quot;: {
                &quot;$toDate&quot;: {
                    &quot;$subtract&quot;: [
                         { &quot;$toLong&quot;: &quot;$connection&quot; },
                         { &quot;$mod&quot;: [ { &quot;$toLong&quot;: &quot;$connection&quot; }, 1000 * 60 * 15 ] }
                     ]
                 }
             },
          &quot;count&quot;: { &quot;$sum&quot;: 1 },
          &quot;user&quot;: {&quot;$addToSet&quot;:&quot;$user&quot; }
        }
     },
     { $sort: {_id: 1} }
  ])
  </code></pre>
  <p>Is it possible to achieve this with mongo only, without javascript ?</p>