Savvy Mongo query selector: exists

MongoDB supports a rich query language; in fact, its support of dynamic queries is one of its more distinguishing features compared to other datastores in the NoSQL world. Mongo’s query language has a variety of query selectors that enable you to fashion some powerful document searches. One particular query selector that comes in handy from time to time is $exists.

Because Mongo is document oriented and thus lacks a rigid schema, documents (for better or for worse) can have varying structures within a collection. In practice, you probably don’t see vastly differing documents within a collection; however, from time to time, various document fields might differ (as in, they might not be present). Take for example, the classic example of a business card: some cards (i.e. documents) might list a fax number while others might omit it. As another example, as an application and its corresponding data evolves, new fields might be added (or removed).

The $exists query selector facilitates finding documents that have (or do not have) specific fields. On more than one occasion, I’ve employed this query selector to find documents in need of a surgical update. For example, in a collection dubbed words with word documents that each contain an embedded definitions document collection, I can find those particular definitions that do not have a part_of_speech element like so:

$exists query selector in action
<span class='line-number'>1</span>
<code class='javascript'><span class='line'><span class="nx">db</span><span class="p">.</span><span class="nx">words</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span><span class="s2">"definitions.part_of_speech"</span><span class="o">:</span><span class="p">{</span><span class="s2">"$exists"</span><span class="o">:</span><span class="kc">false</span><span class="p">}}).</span><span class="nx">sort</span><span class="p">({</span><span class="s2">"spelling"</span><span class="o">:</span><span class="mi">1</span><span class="p">}).</span><span class="nx">limit</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>

Note that the $exists query selector takes a boolean – true or false.

As you can probably ascertain, Mongo’s $exists is slightly different than SQL’s exists – in fact, in SQL there is no way to fashion a query to find a row not containing a column! Can you dig it?