db.products.find().forEach(function(product) {

    var reviewTitle2sku;
    var reviewTitlesInfo = [];

    // RED THE CURRENT FIELD
    reviewTitle2sku = product.reviewTitle2sku;
    if (reviewTitle2sku) {

        // MANIPULATE THE CURRENT FIELD AND CREATE NEW FIELD
        addNewObjectsToArray(reviewTitle2sku, reviewTitlesInfo);

        //UPDATE THE PRODUCT WITH THE NEW FIELD
        db.products.update({ // QUERY
            _id: product._id
        }, { // UPDATE
            $set: {
                'reviewTitlesInfo': reviewTitlesInfo
            }
        });
    }

    function findTitle(o, recomposedString) {
        for (i in o) {
            if (!!o[i] && typeof(o[i]) == "object") {
                recomposedString = recomposedString + '.' + i;
                return findTitle(o[i], recomposedString);
            } else if (typeof(o[i]) == "string") {
                recomposedString = recomposedString + '.' + i;
                return recomposedString.substr(1);
            }
        }
    }

    function findSKU(o, recomposedString) {
        for (i in o) {
            if (!!o[i] && typeof(o[i]) == "object") {
                return findSKU(o[i], recomposedString);
            } else if (typeof(o[i]) == "string") {
                return o[i];
            }
        }
    }

    function addNewObjectsToArray(reviewTitle2sku, reviewTitlesInfo) {
        for (var key in reviewTitle2sku) {
            if (reviewTitle2sku.hasOwnProperty(key)) {
                //reviewTitlesInfo.push({'reviewTitle': key, 'sku': reviewTitle2sku[key]})
                if (typeof reviewTitle2sku[key] === 'object') {
                    var newTitle = findTitle(reviewTitle2sku[key], '');
                    var newSKU = findSKU(reviewTitle2sku[key], '');
                    newObj = {
                        'title': newTitle,
                        'sku': newSKU
                    }
                    reviewTitlesInfo.push(newObj);
                } else if (typeof reviewTitle2sku[key] === 'string') {
                    var newTitle = key;
                    var newSKU = reviewTitle2sku[key];
                    newObj = {
                        'title': newTitle,
                        'sku': newSKU
                    }
                    reviewTitlesInfo.push(newObj);
                }
            }
        }
    }

});

db.products.update({

}, {
    $unset: {
        reviewTitle2sku: null
    }
}, false, true);