Savvy Mongo query selector: exists

how-to
Jan 4, 20133 mins

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>
</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?

andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author