Featured Posts

The 10 Worst Things about World of Warcraft - Mists... I've been playing WoW since vanilla version starting in 2006.  Except for a six-month hiatus in late 2011, I've been a daily player.  I've seen multiple patches come...

Read more

Best Breakfast Burritos, ever! I like eating a good breakfast, usually around lunchtime once I've had my fill of coffee and am awake enough to appreciate a good breakfast. This is my recipe for my ultimate...

Read more

Testing Arrays in PHP - Back to Basics... Sometimes, when you're wallowing through your abstraction class layers, you find yourself using code for simple functions that are normally the focus of an Intro to Programming...

Read more

PHP: Comparing Object Structures I'm working on a project where I am converting an established REST API over to a rabbitMQ service.  Because, you know, dinosaur, I'm continuing to use PHP as my language...

Read more

Mountain Lion and Tunnelblick - Playing Nice Together One of the things that requires some tweaking after the installation of Mac OS X (Mountain Lion) is Tunnelblick, a free and open-source GUI for openVPN.  I use Tunnelblick...

Read more

Subscribe

Searching embedded array structures in Mongodb documents…

Category : Technical
No Gravatar

I am working with a fairly complex data structure, which I am storing in mongodb.  The document, as these are known in the parlance of Mongo, contains six embedded documents, one of which is an array of documents.

It is this document, the array, which I am interested in searching.

The mongo home page is a great resource for us mongo-nubs — the documentation available there is well-written, clear, and concise.  Sometimes, I can even find what I need, before I even realize that I need it!

However, like most documentation, and this is the former support-engineer in me talking, developer documentation is written in a tone of a “solution” and not as a “problem”.  In other words, when I’m looking to solve a problem I am having, I am searching in the terms of my problem.  Once I’ve solved the problem, I will usually document what I’ve learned — the challenge is to not write the document using the vocabulary of the solution, but to use the vocabulary of the problem (seeking) instead.

Anyway, the problem statement for this entry is to be able to search a field within an array of fields within an embedded document, within a mongodb document.  Here we go….

My data structure looks something like this:

In PHP, I’d access the element using a syntax similar to:

$membership['members'][$index]['last_name'];

Assuming that I’m spinning through the data-structure under some looping control mechanism.  What threw me off here, when accessing the structure using Mongo’s BSON notation, was the concept of the array index.

Basically, from my understanding, there is none.  The Mongo webs site quote:

Reaching into arrays is implicit: if the field being queried is an array, the database automatically assumes the caller intends to look for a value within the array…

…gave me the clue I needed to write the query correctly:  you don’t assume an array index since Mongo does it for you.

db.repos.testdata.find{'members.last_name':'Shallop'}.skip(0).limit(30)

fetches all records where the last name = ‘Shallop’ when that last name appears in any of the 10 array positions within the ‘members’ structure.

While I don’t believe that the full impact of this has settled in, I immediately appreciate the aspect of not having to worry about array indexing while traversing deeper into my Mongo documents…

Niiiiiice.

Comments are closed.

Page optimized by WP Minify WordPress Plugin

Our weather forecast is from Wunderground for WordPress