Mongoose: How to use allowDiskUse in mongoose aggregate query

Created on 18 Sep 2018  路  1Comment  路  Source: Automattic/mongoose

I tried this

Mycollection.aggregate([
        {$match: {"deviceId": {$in: graph.devices}, timestamp: {
            $gte:new Date(req.query.afterDate ? req.query.afterDate*1 : 0),
            $lt: new Date(req.query.beforeDate ? req.query.beforeDate*1 : Date.now())
        }}},
        {$sort:{timestamp:-1}},
        {$group: {_id: "$deviceId", data: {$push: "$data"}, timestamp: {$push: "$timestamp"}}}],
        {
            allowDiskUse: true
        },
        function (err, docs)
        {
            if (err)
            {
                logger.error('Error while fetching data for graphId ' + graph._id, {error: err});
                return res.status(404).send({
                    message: 'Error while fetching data for graphId ' + graph._id
                });
            }
            else
            {
                return res.json(docs);
            }
        }
    );

i tried this it throws me an error stating

Error: Arguments must be aggregate pipeline operators

Most helpful comment

@srinivasyl you can use the helper method .allowDiskUse(). The API docs for allowDiskUse are Here

Here is a simple example:

7021.js

#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  name: String
});

const Test = mongoose.model('test', schema);
const tests = '12345'.split('').map(n => { return { name: `test${n}` }; });

async function run() {
  await conn.dropDatabase();
  await Test.create(tests);
  let agg = Test
    .aggregate([{ $match: { name: /[24]$/ } }])
    .allowDiskUse(true);
  assert.deepStrictEqual(agg.options, { allowDiskUse: true });
  let docs = await agg.exec();
  assert.strictEqual(docs.length, 2);
  console.log('All Assertions Pass.');
  return conn.close();
}

run();

Output:

issues: ./7021.js
All Assertions Pass.
issues:

>All comments

@srinivasyl you can use the helper method .allowDiskUse(). The API docs for allowDiskUse are Here

Here is a simple example:

7021.js

#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const schema = new Schema({
  name: String
});

const Test = mongoose.model('test', schema);
const tests = '12345'.split('').map(n => { return { name: `test${n}` }; });

async function run() {
  await conn.dropDatabase();
  await Test.create(tests);
  let agg = Test
    .aggregate([{ $match: { name: /[24]$/ } }])
    .allowDiskUse(true);
  assert.deepStrictEqual(agg.options, { allowDiskUse: true });
  let docs = await agg.exec();
  assert.strictEqual(docs.length, 2);
  console.log('All Assertions Pass.');
  return conn.close();
}

run();

Output:

issues: ./7021.js
All Assertions Pass.
issues:
Was this page helpful?
0 / 5 - 0 ratings