Mongodb 将AllowDiskUse(true)添加到聚合

Mongodb 将AllowDiskUse(true)添加到聚合,mongodb,mongoose,Mongodb,Mongoose,我收到一个“排序超出内存限制…”错误,它指示在聚合中使用allowDiskUse(true)。我的问题是,我不太清楚在哪里可以将其添加到我的代码中。我尝试将其作为管道中的对象和aggregate()方法调用中的属性添加,但在运行时两者都出现错误 代码如下: server.get('/cheap-flight-by-route', function (req, res, next) { Flights.aggregate( {$sort: {'fare.total_pri

我收到一个“排序超出内存限制…”错误,它指示在聚合中使用allowDiskUse(true)。我的问题是,我不太清楚在哪里可以将其添加到我的代码中。我尝试将其作为管道中的对象和aggregate()方法调用中的属性添加,但在运行时两者都出现错误

代码如下:

server.get('/cheap-flight-by-route', function (req, res, next) {

    Flights.aggregate(
        {$sort: {'fare.total_price': 1}},
        {$lookup: {
            from: 'travelroutes',
            localField: 'route',
            foreignField: '_id',
            as: 'routes'
        }},
        {$match: {
            'routes._id': {$exists: true}
        }},
        {$group: {
                _id: {
                    departureAirport: '$routes.departureAirport',
                    arrivalAirport: '$routes.arrivalAirport',
                },
                total_price: {$min: '$fare.total_price'},
                avg_price: {$avg: '$fare.total_price'},
                created: {$first: '$created'},
                doc: {$first: '$$ROOT'}
            }
        },
        {$project: {
            departureAirport: {$arrayElemAt: ['$_id.departureAirport', 0]},
            arrivalAirport: {$arrayElemAt: ['$_id.arrivalAirport', 0]},
            created : '$created',
            price: '$total_price',
            averagePrice: '$avg_price',
            'doc': 1,
            '_id': 0
        }},
        {$sort: {
            'created': 1,
            'departureAirport': 1,
            'arrivalAirport': 1
            },
        },
        function(err, cheapFlights){
            if (err) {
                log.error(err)
                return next(new errors.InvalidContentError(err.errors.name.message))
            }
            res.send(cheapFlights)
            next()
        }
    )  // <-- if I add a .allowDiskUse(true) here it throws a 'bad property' error
})
server.get(“/cheap flight by route”,函数(req,res,next){
航班总数(
{$sort:{'fare.total_price':1},
{$lookup:{
来自:“旅行路线”,
localField:'路由',
foreignField:“\u id”,
as:‘路线’
}},
{$match:{
'routes.\u id':{$exists:true}
}},
{$group:{
_身份证:{
出发机场:“$routes.departureAirport”,
arrivalAirport:“$routes.arrivalAirport”,
},
总价:{$min:'$fare.total_price'},
平均价格:{$avg:'$fare.total_price'},
已创建:{$first:'$created'},
文档:{$first:'$$ROOT'}
}
},
{$项目:{
出发机场:{$arrayElemAt:['$\u id.departureAirport',0]},
arrivalAirport:{$arrayElemAt:['$\u id.arrivalAirport',0]},
已创建:“$created”,
价格:“$总价”,
平均价格:“$avg_价格”,
“文件”:1,
“\u id”:0
}},
{$sort:{
“已创建”:1,
“出发机场”:1,
“到达端口”:1
},
},
功能(错误、错误){
如果(错误){
日志错误(err)
返回下一步(新错误。InvalidContentError(err.errors.name.message))
}
res.send(廉价航班)
下一个()
}

)//尝试在函数后添加第二个聚合参数

`
    function(err, cheapFlights){
        if (err) {
            log.error(err)
            return next(new errors.InvalidContentError(err.errors.name.message));
        }
        res.send(cheapFlights);
        next();
     }, 
     { allowDiskUse: true }
)
`

尝试在函数后添加第二个聚合参数

`
    function(err, cheapFlights){
        if (err) {
            log.error(err)
            return next(new errors.InvalidContentError(err.errors.name.message));
        }
        res.send(cheapFlights);
        next();
     }, 
     { allowDiskUse: true }
)
`

我对您的代码进行了一些更改,请尝试以下操作:

server.get('/cheap-flight-by-route', function (req, res, next) {
    Flights.aggregate([
        {$sort: {
            'fare.total_price': 1
        } },
        {$lookup: {
            from: 'travelroutes',
            localField: 'route',
            foreignField: '_id',
            as: 'routes'
        } },
        {$match: {
            'routes._id': {$exists: true}
        } },
        {$group: {
            _id: {
                departureAirport: '$routes.departureAirport',
                arrivalAirport: '$routes.arrivalAirport',
            },
            total_price: {$min: '$fare.total_price'},
            avg_price: {$avg: '$fare.total_price'},
            created: {$first: '$created'},
            doc: {$first: '$$ROOT'}
        } },
        {$project: {
            departureAirport: {$arrayElemAt: ['$_id.departureAirport', 0]},
            arrivalAirport: {$arrayElemAt: ['$_id.arrivalAirport', 0]},
            created : '$created',
            price: '$total_price',
            averagePrice: '$avg_price',
            'doc': 1,
            '_id': 0
        } },
        {$sort: {
            'created': 1,
            'departureAirport': 1,
            'arrivalAirport': 1
        } }
    ],
    { 
        allowDiskUse: true
    },
    function (err, cheapFlights) {
        if (err) {
            log.error(err);
            return next(new errors.InvalidContentError(err.errors.name.message));
        }
        res.send(cheapFlights);
        next();
    });
});
或者您可以尝试以下方法:

const JSONStream = require('JSONStream');
server.get('/cheap-flight-by-route', function (req, res) {
    let stream = Flights.aggregate([
        {$sort: {
            'fare.total_price': 1
        } },
        {$lookup: {
            from: 'travelroutes',
            localField: 'route',
            foreignField: '_id',
            as: 'routes'
        } },
        {$match: {
            'routes._id': {$exists: true}
        } },
        {$group: {
            _id: {
                departureAirport: '$routes.departureAirport',
                arrivalAirport: '$routes.arrivalAirport',
            },
            total_price: {$min: '$fare.total_price'},
            avg_price: {$avg: '$fare.total_price'},
            created: {$first: '$created'},
            doc: {$first: '$$ROOT'}
        } },
        {$project: {
            departureAirport: {$arrayElemAt: ['$_id.departureAirport', 0]},
            arrivalAirport: {$arrayElemAt: ['$_id.arrivalAirport', 0]},
            created : '$created',
            price: '$total_price',
            averagePrice: '$avg_price',
            'doc': 1,
            '_id': 0
        } },
        {$sort: {
            'created': 1,
            'departureAirport': 1,
            'arrivalAirport': 1
        } }
    ])
    .cursor()
    .exec();

    res.set('Content-Type', 'application/json');
    stream.pipe(JSONStream.stringify()).pipe(res);
});

我对您的代码进行了一些更改,请尝试以下操作:

server.get('/cheap-flight-by-route', function (req, res, next) {
    Flights.aggregate([
        {$sort: {
            'fare.total_price': 1
        } },
        {$lookup: {
            from: 'travelroutes',
            localField: 'route',
            foreignField: '_id',
            as: 'routes'
        } },
        {$match: {
            'routes._id': {$exists: true}
        } },
        {$group: {
            _id: {
                departureAirport: '$routes.departureAirport',
                arrivalAirport: '$routes.arrivalAirport',
            },
            total_price: {$min: '$fare.total_price'},
            avg_price: {$avg: '$fare.total_price'},
            created: {$first: '$created'},
            doc: {$first: '$$ROOT'}
        } },
        {$project: {
            departureAirport: {$arrayElemAt: ['$_id.departureAirport', 0]},
            arrivalAirport: {$arrayElemAt: ['$_id.arrivalAirport', 0]},
            created : '$created',
            price: '$total_price',
            averagePrice: '$avg_price',
            'doc': 1,
            '_id': 0
        } },
        {$sort: {
            'created': 1,
            'departureAirport': 1,
            'arrivalAirport': 1
        } }
    ],
    { 
        allowDiskUse: true
    },
    function (err, cheapFlights) {
        if (err) {
            log.error(err);
            return next(new errors.InvalidContentError(err.errors.name.message));
        }
        res.send(cheapFlights);
        next();
    });
});
或者您可以尝试以下方法:

const JSONStream = require('JSONStream');
server.get('/cheap-flight-by-route', function (req, res) {
    let stream = Flights.aggregate([
        {$sort: {
            'fare.total_price': 1
        } },
        {$lookup: {
            from: 'travelroutes',
            localField: 'route',
            foreignField: '_id',
            as: 'routes'
        } },
        {$match: {
            'routes._id': {$exists: true}
        } },
        {$group: {
            _id: {
                departureAirport: '$routes.departureAirport',
                arrivalAirport: '$routes.arrivalAirport',
            },
            total_price: {$min: '$fare.total_price'},
            avg_price: {$avg: '$fare.total_price'},
            created: {$first: '$created'},
            doc: {$first: '$$ROOT'}
        } },
        {$project: {
            departureAirport: {$arrayElemAt: ['$_id.departureAirport', 0]},
            arrivalAirport: {$arrayElemAt: ['$_id.arrivalAirport', 0]},
            created : '$created',
            price: '$total_price',
            averagePrice: '$avg_price',
            'doc': 1,
            '_id': 0
        } },
        {$sort: {
            'created': 1,
            'departureAirport': 1,
            'arrivalAirport': 1
        } }
    ])
    .cursor()
    .exec();

    res.set('Content-Type', 'application/json');
    stream.pipe(JSONStream.stringify()).pipe(res);
});

我尝试了你的建议,它给了我以下错误:“参数必须是聚合管道操作符”是否有更标准或更易于管理的方法来编写它,以便我可以对更大的数据集使用它?我尝试了你的建议,它给了我以下错误:“参数必须是聚合管道操作符”是否有更标准或更易于管理的方法来编写此文件,以便我可以对更大的数据集使用它?感谢Yuriy,当我在.cursor()之前添加:.allowDiskUse(true)时,管道示例起了作用-我想您刚刚忘记添加它。感谢您的帮助!感谢Yuriy,当我添加:.allowDiskUse(true)时管道示例起了作用在.cursor()之前-我想你忘了添加它。谢谢你的帮助!