gpt4 book ai didi

mongodb - 将 AllowDiskUse(true) 添加到聚合

转载 作者:行者123 更新时间:2023-12-02 00:43:35 25 4
gpt4 key购买 nike

我收到“排序超出内存限制...”错误,指示在我的聚合中使用 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', 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);
});

关于mongodb - 将 AllowDiskUse(true) 添加到聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com