Your initial post described IDs being incremented every time you insert a new data set, but without regard to any existing sequencing. That's what the code I supplied provides for.
Your code, however, envisages searching through the document for all existing IDs (which could be a slow process in a long document) to find the highest one (which might not be the last one allocated if that one has since been deleted), then (apparently) you intend to sequentially add successive IDs to data presently lacking them.
The two approaches are quite different and yours doesn't really meet your "every time I insert a new data set" criterion.