Join.js
function arrayJoin (results, args) {
            var srcDataArray = results,//use these results as the source of the join
                joinCollection = args.joinCollection,//This is the mongoDB.Collection to use to join on
                joinQuery = args.joinQuery,
                joinType = args.joinType || 'left',
                rightKeys = args.rightKeys || [args.rightKey],//Get the value of the key being joined upon
                newKey = args.newKey,//The new field onto which the joined document will be mapped
                callback = args.callback,//The callback to call at this level of the join

                length,
                i,

                subqueries,
                keyHashBin = {},
                accessors = [],
                joinLookups = [],
                inQueries = [],
                leftKeys = args.leftKeys || [args.leftKey];//place to put incoming join results

            rightKeys.forEach(function () {
                inQueries.push([]);
            });

            leftKeys.forEach(function (key) {//generate the accessors for each entry in the composite key
                accessors.push(getKeyValueAccessorFromKey(key));
            });

            length = results.length;

            //get the path first
            for (i = 0; i < length; i += 1) {
                buildHashTableIndices(keyHashBin, results[i], i, accessors, rightKeys, inQueries, joinLookups, {});
            }//create the path

            buildQueriesFromHashBin(keyHashBin, rightKeys, 0, [], joinLookups, inQueries);

            if (!Array.isArray(srcDataArray)) {
                srcDataArray = [srcDataArray];
            }

            subqueries = getSubqueries(inQueries, joinLookups, joinQuery, args.pageSize || 25, rightKeys);//example
            runSubqueries(subqueries, function (items) {
                var un;
                performJoining(srcDataArray, items, {
                    rightKeyPropertyPaths: rightKeys,
                    newKey: newKey,
                    keyHashBin: keyHashBin
                });

                if (joinType === "inner") {
                    removeNonMatchesLeft(srcDataArray, newKey);
                }

                if (joinStack.length > 0) {
                    arrayJoin(srcDataArray, joinStack.shift());
                } else {
                    callIfFunction(finalCallback, [un, srcDataArray]);
                }
                callIfFunction(callback, [un, srcDataArray]);
            }, joinCollection);
        }
function arrayJoin (results, args) {
            var srcDataArray = results,//use these results as the source of the join
                joinCollection = args.joinCollection,//This is the mongoDB.Collection to use to join on
                joinQuery = args.joinQuery,
                joinType = args.joinType || 'left',
                rightKeys = args.rightKeys || [args.rightKey],//Get the value of the key being joined upon
                newKey = args.newKey,//The new field onto which the joined document will be mapped
                callback = args.callback,//The callback to call at this level of the join

                length,
                i,

                subqueries,
                keyHashBin = {},
                accessors = [],
                joinLookups = [],
                inQueries = [],
                leftKeys = args.leftKeys || [args.leftKey];//place to put incoming join results

            rightKeys.forEach(function () {
                inQueries.push([]);
            });

            leftKeys.forEach(function (key) {//generate the accessors for each entry in the composite key
                accessors.push(getKeyValueAccessorFromKey(key));
            });

            length = results.length;

            //get the path first
            for (i = 0; i < length; i += 1) {
                buildHashTableIndices(keyHashBin, results[i], i, accessors, rightKeys, inQueries, joinLookups, {});
            }//create the path

            buildQueriesFromHashBin(keyHashBin, rightKeys, 0, [], joinLookups, inQueries);

            if (!Array.isArray(srcDataArray)) {
                srcDataArray = [srcDataArray];
            }

            subqueries = getSubqueries(inQueries, joinLookups, joinQuery, args.pageSize || 25, rightKeys);//example
            runSubqueries(subqueries, function (items) {
                var un;
                performJoining(srcDataArray, items, {
                    rightKeyPropertyPaths: rightKeys,
                    newKey: newKey,
                    keyHashBin: keyHashBin
                });

                if (joinType === "inner") {
                    removeNonMatchesLeft(srcDataArray, newKey);
                }

                if (joinStack.length > 0) {
                    arrayJoin(srcDataArray, joinStack.shift());
                } else {
                    callIfFunction(finalCallback, [un, srcDataArray]);
                }
                callIfFunction(callback, [un, srcDataArray]);
            }, joinCollection);
        }
function arrayJoin ( results , args ) { var srcDataArray = results , //use these results as the source of the join joinCollection = args . joinCollection , //This is the mongoDB.Collection to use to join on joinQuery = args . joinQuery , joinType = args . joinType || 'left' , rightKeys = args . rightKeys || [ args . rightKey ] , //Get the value of the key being joined upon newKey = args . newKey , //The new field onto which the joined document will be mapped callback = args . callback , //The callback to call at this level of the join length , i , subqueries , keyHashBin = { } , accessors = [ ] , joinLookups = [ ] , inQueries = [ ] , leftKeys = args . leftKeys || [ args . leftKey ] ; //place to put incoming join results rightKeys . forEach ( function ( ) { inQueries . push ( [ ] ) ; } ) ; leftKeys . forEach ( function ( key ) { //generate the accessors for each entry in the composite key accessors . push ( getKeyValueAccessorFromKey ( key ) ) ; } ) ; length = results . length ; //get the path first for ( i = 0 ; i < length ; i += 1 ) { buildHashTableIndices ( keyHashBin , results [ i ] , i , accessors , rightKeys , inQueries , joinLookups , { } ) ; } //create the path buildQueriesFromHashBin ( keyHashBin , rightKeys , 0 , [ ] , joinLookups , inQueries ) ; if ( ! Array . isArray ( srcDataArray ) ) { srcDataArray = [ srcDataArray ] ; } subqueries = getSubqueries ( inQueries , joinLookups , joinQuery , args . pageSize || 25 , rightKeys ) ; //example runSubqueries ( subqueries , function ( items ) { var un ; performJoining ( srcDataArray , items , { rightKeyPropertyPaths : rightKeys , newKey : newKey , keyHashBin : keyHashBin } ) ; if ( joinType === "inner" ) { removeNonMatchesLeft ( srcDataArray , newKey ) ; } if ( joinStack . length > 0 ) { arrayJoin ( srcDataArray , joinStack . shift ( ) ) ; } else { callIfFunction ( finalCallback , [ un , srcDataArray ] ) ; } callIfFunction ( callback , [ un , srcDataArray ] ) ; } , joinCollection ) ; }
Begin the joining process by compiling some data and performing a query for the objects to be joined.
@param results The results of the previous join or query
@param args The user supplied arguments which will configure this join
Begin the joining process by compiling some data and performing a query for the objects to be joined .