无法在节点 js 代码中通过猫鼬运行聚合

时间:2021-03-15 18:27:02

标签: node.js mongodb mongoose aggregate-functions pipeline

我们正在尝试运行聚合查询,如下面的节点 js 代码- 这段代码是用 mongo 3.4 版编写的,但是当我们将版本更新到 4.4 时,我们得到了错误 - Mongoose 聚合错误:参数必须是聚合管道运算符

function (callback) {
        query = countrySchema.aggregate
            ([
                { $unwind: '$states' },
                { $match: { $and: matchQuery } },
                { $project: { states: 1, countryName: 1, _id: 1 } },

                { $skip: (page - 1) * count },
                { $limit: count }
            ], {
                cursor: { batchSize: 0 }
            });
        callback()
    },
    function (callback) {
        countQuery = countrySchema.aggregate
            ([
                { $unwind: '$states' },
                { $match: { $and: matchQuery } },
                { $group: { _id: null, count: { '$sum': 1 } } },
                { $project: { _id: 1, count: 1 } }
            ], {
                cursor: { batchSize: 0 }
            })
        callback()
    },
    function (callback) {
        query.exec(function (err, doc) {
            console.log("A", err);
            console.log("B", doc);
            if (!err) {
                countQuery.exec(function (val, resu) {
                    d.result = doc
                    if (resu !== undefined && resu !== null) {
                        if (resu[0] !== undefined && resu[0] !== null) {
                            d.total = resu[0].count;
                        }
                    }
                    callback()
                })
            }
            else {
                d.result = []
                d.total = 0
                callback()
            }
        })
    }

0 个答案:

没有答案