1

I have a basic rhetorical/theoretical question for people experienced using both MongoDB and RDBMS. Surprised SO has no Data Munging or Data Engineering forum.

In making an aggregation pipeline in Mongo, we use $unwind to explode nested array values so each has its own document so that we may group or do other aggregation.

To me this smells just like getting the data to First Normal Form. The documents tend to look just like records in 1NF at this stage. It seems like that is the goal. I have searched and searched and not found that phrase uttered in any Mongo topics, certainly it was not said in courses I took in MongoDB University. And this would apply to other non-relational data systems as well.

Is 1NF essentially the same thing as the data form necessary for an aggregation step? Is there any data manipulation term for it?

2

The documents tend to look just like records in 1NF at this stage.

For an array of primitive values the output of $unwind looks similar to First normal form (1NF), but pedantically the $unwind output should be expected to be UNF (Unnormalised form) since:

  • documents output by $unwind do not have a unique key constraint (although arguably the includeArrayIndex option can be used to provide an explicit row identifier)
  • arrays support complex data types (nested arrays and documents)

I have searched and searched and not found that phrase uttered in any Mongo topics, certainly it was not said in courses I took in MongoDB University.

Database normalisation is a strongly RDBMS concept, where you design a model to reduce data redundancy ("How to efficiently store data"). Standard normal forms are expressions of more rigid data relationships and constraints for tabular data.

MongoDB data model design is application-centric ("How to efficiently use data"). Since the decision on whether to normalise or denormalise data is influenced by the usage context, data model discussion generally focuses on data growth and use case patterns rather than highly normalised forms.

Is 1NF essentially the same thing as the data form necessary for an aggregation step?

1NF (or an $unwind operation) certainly isn't a necessary step for aggregating or transforming arrays. MongoDB has other array expression operators such as $arrayToObject, $filter, and $map which implement different data transformations.

Is there any data manipulation term for it?

I'm not sure if there are any generally accepted terms, but I think an $unwind operation would typically be described as either "flattening" or "unnesting" arrays.

  • Good discussion, and Flattening is certainly a term I have used. I don't think an aggregation will work if the targeted field is in unnormalized form (UNF). The field to be aggregated must have a single value per document and be in one field - atomic. Multivalue entries aren't fully unwound. But now I realize that duplicates are OK, and 1NF must be a set, dupes not allowed, so while it's not first normal form, the shape of the data does have certain requirements and a standard name for it would be nice. – charles ross Oct 22 '18 at 1:51
  • @charlesross Depending on the operator, MongoDB aggregation can work with non-primitive types including arrays. There is no general requirement for a field or expression being aggregated to have a single value so I think the input is inherently UNF (which is a key difference from SQL aggregation). For example, $sum will ignore non-numeric or non-existent fields and can traverse into arrays to add numeric values: db.foo.insert([{a:[1,2,"b"]}, {a:5}, {a: { b: 1}}]); db.foo.aggregate({ $project: { total: { $sum: "$a"}}}) will return totals of 3, 5, and 0. – Stennie Oct 22 '18 at 4:18
  • Thinking further about terminology, the format used in the aggregation pipeline is document-oriented or semi-structured data. Using the data from my previous comment with the $push operator is perhaps a more interesting example: db.foo.aggregate({$group: { _id:null, docs: { $push: "$a"}}}) will aggregate the values of field a into a new array preserving the original values (single value, array, or subdocument). – Stennie Oct 22 '18 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.