将AllowDiskUse(true)添加到聚合

时间:2017-07-18 13:55:50

标签: mongodb mongoose

我得到超出内存限制......'在我的聚合中使用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
})

3 个答案:

答案 0 :(得分:1)

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

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);
});

答案 1 :(得分:0)

尝试在函数后添加,作为第二个聚合参数。

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

答案 2 :(得分:0)

MongoClient.connect("mongodb://localhost:27017/test", function(err, db) {
    // Get an aggregation cursor
    var cursor = db.collection('data').aggregate([
            {$match: {}}
        ], {
        allowDiskUse: true
      , cursor: {batchSize: 1000}
        });

    // Use cursor as stream
    cursor.on('data', function(data) {
        console.dir(data);
    });

    cursor.on('end', function() {
        db.close();
    });
});