title: MongoDB distinct with grouping
body: <p>I am brand new in the mongo world. To the degree where I cannot really formulate my questions properly. But here goes...
  So here is my best attempt...</p>
  <pre><code>db.t2.insertOne( { animal: &quot;cat&quot;, action: &quot;meow&quot;});
  db.t2.insertOne( { animal: &quot;cat&quot;, action: &quot;stalk&quot;});
  db.t2.insertOne( { animal: &quot;cat&quot;, action: &quot;claw&quot;});
  db.t2.insertOne( { animal: &quot;dog&quot;, action: &quot;woof&quot;});
  db.t2.insertOne( { animal: &quot;dog&quot;, action: &quot;beg&quot;});
  db.t2.insertOne( { animal: &quot;fish&quot;, action: &quot;swim&quot;});
  </code></pre>
  <p>The data is pretty flat, right?</p>
  <pre><code>db.t2.find();
  { _id: ObjectId(&quot;61142a398f4cfec27b46e89d&quot;),
    animal: 'cat',
    action: 'meow' }
  { _id: ObjectId(&quot;61142a438f4cfec27b46e89e&quot;),
    animal: 'cat',
    action: 'stalk' }
  { _id: ObjectId(&quot;61142a4a8f4cfec27b46e89f&quot;),
    animal: 'cat',
    action: 'claw' }
  { _id: ObjectId(&quot;61142a7e8f4cfec27b46e8a0&quot;),
    animal: 'dog',
    action: 'woof' }
  { _id: ObjectId(&quot;61142a838f4cfec27b46e8a1&quot;),
    animal: 'dog',
    action: 'beg' }
  { _id: ObjectId(&quot;61142a918f4cfec27b46e8a2&quot;),
    animal: 'fish',
    action: 'swim' }
  </code></pre>
  <p>I'm looking for a way to query the data more compressed.
  Something like...</p>
  <pre><code>[{
  &quot;animal&quot;:&quot;cat&quot;,
  &quot;action&quot;:[&quot;meow&quot;, &quot;stalk&quot;, &quot;claw&quot;]
  },
  &quot;animal&quot;:&quot;dog&quot;,
  &quot;action&quot;:[&quot;woof&quot;, &quot;beg&quot;]
  },
  &quot;animal&quot;:&quot;fish&quot;,
  &quot;action&quot;:[&quot;swim&quot;]
  }]
  </code></pre>