In this blog post which is part of the series "A Developer's Notes", we'd like to record useful MongoDB operations in regards to converting an array (collection) to an object, as this is a common problem developers face while working with MongoDB.
Introduction
When it comes to model your data with MongoDB you've got to decide between embedding vs. referencing. Before we briefly touch on these two terms, let's talk about the fundamentals.
In MongoDB compared to any other relational database system, your data is stored as documents in collections, rather than rows and tables.
By embedding data directly in documents, you might break out of the third normal form (3NF), to gain speed and reduce complexity. Whereas with referencing you split data over multiple documents, which has its pros and cons, too. We won't get too deep into those in this blog post, but we wanted to touch briefly on the basics.
The problem
What if you did decide to go with embedding to store several information in one document? More precisly as an array of objects. But you realise that there's an application, or more generalised spoken, a use case in which you need the data as an object instead of an array.
Well, MongoDB has a solution for that, the $arrayToObject operator.
Let's look at the following example document:
The property "sensorValues" is an array and contains multiple different sensor values. This has immense impact on your index strategy, because it reduces the amount indexes you need to find sensors by their values. But more about this aspect in a different blog post.
How are we able to change it's document structure directly in the database? At source if you will, without having to do anything in our application. Let's have a look at that now.
The solution
The MongoDB Aggregation Pipeline, which you can use to retrieve documents from the database, has got a little nifty helper called $arrayToObject.
This even works in an update operation, in case you want to generate this additional property and store it back to your documents.
Given our example document from above this produces following outcome document:
The steps involved
$map(ping) every object from "sensorValues" to a simple array with 2 elements. The first element is the key and the second element is the value. This will be the input argument to $arrayToObject later
$reduce the array of arrays (result from 1.) by invoking $arrayToObject on every element...
...and merging these single property objects into the final "sensorValues" object with $mergeObjects
In $reduce "$$this" contains the current reduced element, whereas "$$value" contains the final product, in our case the "sensorValue" object
Conclusion
Being familiar with the above approach of converting an array to an object in MongoDB opens up new possibilities in regards to data modelling. We're going to look into this at more detail in another blog post. Please come back later.
We hope this is of value for all you Mongos out there.
Thank you very much.
Photo by Karen Vardazaryan on Unsplash