jstests/aggregation/extras/utils.js
/**
 * Compare two documents for equality. Returns true or false. Only equal if they have the exact same
 * set of properties, and all the properties' values match except the values with names in the
 * fieldsToSkip array. The fields in fieldsToSkip will be skipped at all levels of the document.
 * The value comparison with the recursive anyEq function allows for comparing nested array values
 * ignoring the elements' order.
 * If the order of the nested arrays elements is significant for the equivalence, the assert.docEq
 * from assert.js should be used instead.
 */
Compare two documents for equality. Returns true or false. Only equal if they have the exact same
set of properties, and all the properties' values match except the values with names in the
fieldsToSkip array. The fields in fieldsToSkip will be skipped at all levels of the document.
The value comparison with the recursive anyEq function allows for comparing nested array values
ignoring the elements' order.
If the order of the nested arrays elements is significant for the equivalence, the assert.docEq
from assert.js should be used instead.
Compare two documents for equality . Returns true or false . Only equal if they have the exact same set of properties and all the properties ' values match except the values with names in the fieldsToSkip array . The fields in fieldsToSkip will be skipped at all levels of the document . The value comparison with the recursive anyEq function allows for comparing nested array values ignoring the elements ' order . If the order of the nested arrays elements is significant for the equivalence the assert . docEq from assert . js should be used instead .
function documentEq(dl, dr, verbose = false, valueComparator, fieldsToSkip = []) {
    const debug = msg => verbose ? print(msg) : null;  
    if (!(dl instanceof Object)) {
        debug('documentEq:  dl is not an object ' + tojson(dl));
        return false;
    }
    if (!(dr instanceof Object)) {
        debug('documentEq:  dr is not an object ' + tojson(dr));
        return false;
    }
    for (let propertyName in dl) {
        if (!dl.hasOwnProperty(propertyName))
            continue;
        if (fieldsToSkip.includes(propertyName))
            continue;
        if (!dr.hasOwnProperty(propertyName)) {
            debug('documentEq: dr doesn\'t have property ' + propertyName);
            return false;
        }
        if (!anyEq(dl[propertyName], dr[propertyName], verbose, valueComparator, fieldsToSkip)) {
            return false;
        }
    }
    for (let propertyName in dr) {
        if (!dr.hasOwnProperty(propertyName))
            continue;
        if (fieldsToSkip.includes(propertyName))
            continue;
        if (!dl.hasOwnProperty(propertyName)) {
            debug('documentEq: dl is missing property ' + propertyName);
            return false;
        }
    }
    debug(`documentEq: these are equal: ${tojson(dl)} == ${tojson(dr)}`);
    return true;
}
function documentEq ( dl , dr , verbose = false , valueComparator , fieldsToSkip = [ ] ) { const debug = msg => verbose ? print ( msg ) : null ; if ( ! ( dl instanceof Object ) ) { debug ( 'documentEq:  dl is not an object ' + tojson ( dl ) ) ; return false ; } if ( ! ( dr instanceof Object ) ) { debug ( 'documentEq:  dr is not an object ' + tojson ( dr ) ) ; return false ; } for ( let propertyName in dl ) { if ( ! dl . hasOwnProperty ( propertyName ) ) continue ; if ( fieldsToSkip . includes ( propertyName ) ) continue ; if ( ! dr . hasOwnProperty ( propertyName ) ) { debug ( 'documentEq: dr doesn\'t have property ' + propertyName ) ; return false ; } if ( ! anyEq ( dl [ propertyName ] , dr [ propertyName ] , verbose , valueComparator , fieldsToSkip ) ) { return false ; } } for ( let propertyName in dr ) { if ( ! dr . hasOwnProperty ( propertyName ) ) continue ; if ( fieldsToSkip . includes ( propertyName ) ) continue ; if ( ! dl . hasOwnProperty ( propertyName ) ) { debug ( 'documentEq: dl is missing property ' + propertyName ) ; return false ; } } debug ( ` ${ tojson ( dl ) } ${ tojson ( dr ) } ` ) ; return true ; }
Compare two documents for equality.
Compare two documents for equality .
// Helper to log 'msg' iff 'verbose' is true. // Make sure these are both objects. // Start by checking for all of dl's properties in dr. // Skip inherited properties. // The documents aren't equal if they don't both have the property. // Now make sure that dr doesn't have any extras that dl doesn't have. // If dl doesn't have this they are not equal; if it does, we compared it above and know it // to be equal.