async function()

in routes/doc.js [252:339]


        async function (req, res) {
            if (req.query.f) {
                var f = req.query.f;
                if (!Array.isArray(f)) {
                    f = [f];
                }

                var pipeLine = normalizeQuery(req.querymen.query);
                var prj = {};
                for (var k of f) {
                    var options = opts.facet[k];
                    if (options) {
                        if (Array.isArray(options.path)) {
                            prj[k] = { "$setUnion": [options.path.map(x => { return '$' + x })] }
                        } else if (typeof options.path === 'string') {
                            prj[k] = '$' + options.path;
                        } else if (Object.keys(options.path).length != 0) {
                            prj[k] = options.path;
                        }
                    }
                }
                if (Object.keys(prj).length > 0) {

                    var g = {},
                        gg = {},
                        sor = {};

                    if (typeof f !== 'string' && f.length == 1) {
                        g = '$' + f[0];
                    } else {
                        for (var k of f) {
                            g[k] = '$' + k;
                            sor[k] = 1;
                            gg[k] = '$_id.' + k;
                        }
                    }

                    pipeLine = pipeLine.concat([{
                        $project: prj
                    },
                    {
                        $group: {
                            _id: g,
                            t: {
                                $sum: 1
                            }
                        }
                    }
                    ]);
                    gg.t = '$t';
                    if (f[1] && !req.query.ungroup) {
                        var s = {};
                        s["_id." + f[1]] = 1;
                        pipeLine.push({
                            $sort: s
                        });
                        delete gg[f[0]];
                        pipeLine.push({
                            $group: {
                                _id: '$_id.' + f[0],
                                t: {
                                    $sum: '$t'
                                },
                                items: {
                                    $push: gg
                                }
                            }
                        })
                    }

                    if (req.querymen.cursor.sort) {
                        pipeLine.push({
                            $sort: {
                                '_id': 1
                            }
                        })
                    }
                    //console.log('pipeLine:' + JSON.stringify(pipeLine,2,2,2));
                    var ret = await Document.aggregate(pipeLine);

                    res.json(ret);
                } else {
                    res.json([{ "_id": "Error: Wrong field specification", "t": 404 }]);
                }
            } else {
                res.json([]);
            }
        });