function normalizeQuery()

in routes/doc.js [340:404]


    function normalizeQuery(q) {
        //console.log('GOT' + JSON.stringify(q,2,2,2));
        var pipeLine = [];
        if (q['$text']) {
            if (q['$text']['$in']) {
                var terms = "";
                for (var term of q['$text']['$in']) {
                    terms = terms + ' ' + term;
                }
                delete q['$text']['$in'];
                q['$text']['$search'] = terms;
            }
            if (q['$text']['$search']) {
                var ids = q['$text']['$search'].match(RegExp(idpattern, 'img'));
                if (ids && ids.length) {
                    var idq = {};
                    q[idpath] = { "$in": ids }
                    delete q['$text'];
                }
            }
        }
        // Translate queries for empty strings to match any of "", null, non-existant.
        for (var p in q) {
            if (q[p] && q[p]['$in'] && q[p]['$in'].includes('null')) {
                q[p]['$in'].push("");
                q[p]['$in'].push(null);
            }
            if (q[p] === '') {
                q[p] =
                {
                    "$not": {
                        "$exists": true,
                        "$nin": ['', null]
                    }
                };
                //{"$not":{"$exists": true, $ne: ""}}
                //q[p] = {"$in":["",null]};
            }
            if (q[p] === 'null') {
                //req.querymen.query[q] = {"$exists": false}
                q[p] = { "$in": ["", null] }
            }
        }
        var lookups = {};
        if (Array.isArray(opts.conf.lookup) && opts.conf.lookup.length > 0) {
            var lookupAsNames = {};
            for (var l of opts.conf.lookup) {
                lookupAsNames[l.$lookup.as] = true;
            }
            for (var p in q) {
                var a = p.split('.', 1)[0];
                if (lookupAsNames[a]) {
                    lookups[p] = q[p];
                    delete q[p];
                }
            }
            pipeLine = pipeLine.concat(opts.conf.lookup)
            if (Object.keys(lookups).length != 0) {
                pipeLine.push({ "$match": lookups });
            }
        }
        pipeLine.unshift({ "$match": q });
        //console.log('PIPEline' + JSON.stringify(pipeLine,2,2,2));
        return pipeLine;
    };