Filtering By “Reaching” Into Arrays With Mongo

One seriously neat capability with Mongo is the ability to filter by “reaching” into an array and filtering on a property of an object in an array.

This is seriously powerful stuff and incredibly easy with Mongo.  There’s an example in the official SDK docs, but it’s buried pretty deeply in there.

So here’s a short summary. Say we are building an address book.

From the Mongo shell, we would enter:

To create the “addressbook” repository.

Next, we insert two contacts:

So what if we want to find the list of all contacts who have an address in the state “NY”?

In Mongo, this can be accomplished with the following query:

Love that simplicity.

You may also like...