Here’s how I return a list of keys from an associative array (i.e. an embedded document) without also returning their value.
The data structure I have is shown below. It’s made up of an associative array [friends], which contains a set of keys (Twitter usernames) and values (details about the user). My aim was to return a list of the Twitter usernames, without all of the details associated to them which – in some cases – could be quite a large amount of data.
What this essentially does is:
- Line 3 – Here I specify the document I want to apply the group function to. In my case I just want to return a list of keys from a specific document, thus I identify it using its document id.
- Line 4 – I create an empty array – results – into which the keys are added.
- Line 5 – Here I start to define the reduce function. It takes two arguments.
- obj – The current document (In my case there is only one).
- prev – This object contains all the variables defined on Line 4. In this case that’s just results.
- Line 8 – I simply push the key – key – onto the results array.
…and that’s it! The result looks like:
The reason that results is wrapped in an array is that the function will return one results array per Document iterated over. In my case that’s just one, but had I used a condition that returned more documents, they would all be included within this wrapping array.
Doing this in PHP
Things to note:
- According to the MongoDB documentation, the group function does not work in a sharded environment.
- This query can’t use any indexes, with the exception of finding the document set to iterate over, i.e. the query defined under “cond“. For my problem this isn’t likely to ever become an issue, but it might if you start iterating over a large number of documents.
This is just how I solved it. There may well be better solutions. If you know of one, please feel free to share it with us all. 🙂