Skip to content
Advertisement

Generalize algorithm for a loop comparing to last record?

I have a data set which I can represent by this toy example of a list of dictionaries:

JavaScript

Here is an algorithm in Python which collects each of the keys in each ‘collection’ and whenever there is a key change, the algorithm adds those keys to output.

JavaScript

The correct output given here records each change of field name: Foo, Bar, Baz as well as the change of case in field start_date to Start_Date:

JavaScript

Is there a general algorithm which covers this pattern comparing current to previous item in a stack?

I need to generalize this algorithm and find a solution to do exactly the same thing with MongoDB documents in a collection. In order for me to discover if Mongo has an Aggregation Pipeline Operator which I could use, I must first understand if this basic algorithm has other common forms so I know what to look for.

Or someone who knows MongoDB aggregation pipelines really well could suggest operators which would produce the desired result?

Advertisement

Answer

EDIT: If you want to use a query for this, one option is something like:

  1. The $objectToArray allow to format the keys as values, and the $ifNull allows to check several options of start_date.
  2. The $unwind allows us to sort the keys.
  3. The $group allow us to undo the $unwind, but now with sorted keys
  4. $reduce to create a string from all keys, so we’ll have something to compare.
  5. group again, but now with our string, so we’ll only have documents for changes.
JavaScript

Playground example

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement