Get distinct first words in a string
<p>I try to get distinct first words in a field in a database:</p>

<pre><code>db.mycollection.distinct(substr(0, city.indexOf(' ')),{"state":"FirstState"})
</code></pre>

<p>But that does not work obviosly, but maybe it helps to get an idea about what I want to do.</p>

<p>Say I have two documents:</p>

<pre><code>{
  "_id": "10280",
  "city": "FirstCity is great",
  "state": "FirstState",
  "pop": 2224
},
{
  "_id": "10282",
  "city": "SecondCity even greater",
  "state": "FirstState",
  "pop": 5574
}
</code></pre>

<p>I then want to get:</p>

<pre><code>["FirstCity","SecondCity"]
</code></pre>

<p>I.e. get distinct values of <code>city</code> field's <code>first word</code> in array.
Is there any way to do that?</p>

<p>Best Regards</p>