// mongo-redact.js
//
// A mongo shell function to redact sensitive information while 
// retaining the shape of the document.
//
// Jon Rangel, January 2015


// Note:
// - ints and floats are replaced with 999
// - longs are replaced with 999999
// - strings are replaced with "XXXXXXXX"


function redactValue(val) {
    if (typeof val == "number") {
        if (val === (val|0)) {
            return 999;
        } else {
            return 999.123;
        }
    } else if (val instanceof NumberLong) {
        return new NumberLong(999999);
    } else if (typeof val == "string") {
        return "XXXXXXXX";
    } else if (val instanceof Date) {
        // return the same date
    } else if (val instanceof ObjectId) {
        // return the same Object ID
    } else if (val instanceof Array) {
        return redactArray(val);
    } else if (val instanceof Object) {
        return redactDoc(val);
    }

    return val;
}


function redactArray(arr) {
    for (var i = 0; i < arr.length; i++) {
        arr[i] = redactValue(arr[i]);
    }

    return arr;
}


function redactDoc(doc) {
    for (var prop in doc) {
        if (doc.hasOwnProperty(prop)) {
            doc[prop] = redactValue(doc[prop]);
        }
    }

    return doc;
}


function printRedactedDoc(doc) {
    printjson(redactDoc(doc));
}

@@ -593,25 +593,115 @@
     
-return %22XXXXXXXX%22
+var s = %22%22;%0A        for (var i = 0; i %3C val.length; i++) %7B%0A            s += %22X%22;%0A        %7D%0A        return s
 ;%0A  

Preserve original string length