title: Mongodb distinct on a array field with regex query?
body: <p>Basically i'm trying to implement tags functionality on a model.</p>
  
  <pre><code>&gt; db.event.distinct("tags")
  [ "bar", "foo", "foobar" ]
  </code></pre>
  
  <p>Doing a simple distinct query retrieves me all distinct tags. However how would i go about getting all distinct tags that match a certain query? Say for example i wanted to get all tags matching <code>foo</code> and then expecting to get <code>["foo","foobar"]</code> as a result?</p>
  
  <p>The following queries is my failed attempts of achieving this:</p>
  
  <pre><code>&gt; db.event.distinct("tags",/foo/)
  [ "bar", "foo", "foobar" ]
  
  &gt; db.event.distinct("tags",{tags: {$regex: 'foo'}})
  [ "bar", "foo", "foobar" ]
  </code></pre>