question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method "collection.aggregate()" accepts at most two arguments

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? Error: Method "collection.aggregate()" accepts at most two arguments

MongoInvalidArgumentError: Method "collection.aggregate()" accepts at most two arguments
    at Collection.aggregate (/home/dave/projects/asd/node_modules/mongodb/lib/collection.js:367:19)
    at NativeCollection.<computed> [as aggregate] (/home/dave/projects/asd/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:200:33)
    at NativeCollection.Collection.doQueue (/home/dave/projects/asd/node_modules/mongoose/lib/collection.js:135:23)
    at /home/dave/projects/asd/node_modules/mongoose/lib/collection.js:82:24
    at processTicksAndRejections (internal/process/task_queues.js:77:11)

If the current behavior is a bug, please provide the steps to reproduce.

    import Client from '../models/client';
    ...
    const clients = await Client.aggregate([
      { $match: { active: true } },
      { $project: {
          name: 1,
          image: 1,
          maxContract: {
            $arrayElemAt: [ '$contracts', { $indexOfArray: ['$contracts.endDate', { $max: '$contracts.endDate' }] } ]
          }
        }
      },
      { $match: { 'maxContract.endDate': { $lte: date, $gte: new Date() } } },
      { $sort: { 'maxContract.endDate': 1 } }
    ]);

Happens also with a simple match query inside aggregate:

    import Client from '../models/client';
    ...
    const clients = await Client.aggregate([
      { $match: { active: true } }
    ]);

What is the expected behavior? Working as v5?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node: v14.17.6, Mongoose: v6.0.5, TypeScript: v4.2.4

I have this error since v6, it’s working fine with v5. I can’t find where the breaking change is documented tbh… Any help is appreciated, thanks.

Also, TypeScript aggregate signature is:

(method) Model<IClient, {}, {}>.aggregate<any>(pipeline?: any[]): Aggregate<any[]> (+1 overload)

But if the arguments must be different, TypeScript should tell before compiling.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Sep 18, 2021

Confirmed this bug, fix will be in 6.0.7 on Monday 👍

1reaction
IslandRhythmscommented, Sep 16, 2021

Modify script below to reproduce your error and I will try again.

import * as mongoose from 'mongoose';

const ClientSchema = new mongoose.Schema({
  name: { type: String, required: true, trim: true },
  image: { type: String, default: '' },
  contracts: [{
    id: Number,
    name: String,
    startDate: Date, 
    endDate: Date,
    created_at: Date,
    updated_at: Date
  }],
  active: { type: Boolean, default: true },
});

export interface IClient extends Document {
  _id: any;
  name: string;
  image: string;
  contracts: any[];
  active: boolean;
}

const Client = mongoose.model<IClient>('Client', ClientSchema);

async function test() {
    await mongoose.connect('mongodb://localhost:27017/');
    await mongoose.connection.dropDatabase();
    const clients = await Client.aggregate([
        { $match: { active: true } },
        { $project: {
            name: 1,
            image: 1,
            maxContract: {
              $arrayElemAt: [ '$contracts', { $indexOfArray: ['$contracts.endDate', { $max: '$contracts.endDate' }] } ]
            }
          }
        },
        { $match: { 'maxContract.endDate': { $lte: new Date(), $gte: new Date() } } },
        { $sort: { 'maxContract.endDate': 1 } }
      ]);
      
      const test = await Client.aggregate([ { $match: { active: true } }]);
      console.log('done');
}




test();
Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB collection.aggregrate accept only two arguments
When I run that aggregation, it show the error. What am I missing? const data = await Rooms.aggregate([{ $match: { ...
Read more >
[GraphQL error]: Method "collection.aggregate()" accepts at ...
[GraphQL error]: Method "collection.aggregate()" accepts at most two arguments The app uses aldeed:collection2 and …
Read more >
db.collection.aggregate()
See the aggregation pipeline operators for details. The method can still accept the pipeline stages as separate arguments instead of as elements in...
Read more >
Lesson 3: Introducing the MongoDB aggregate method
This is followed by the aggregate method and its two arguments, as indicated by the pipeline and options placeholders.
Read more >
How To Use Aggregations in MongoDB
The aggregate() method executed on the cities collection instructs MongoDB to run an aggregation pipeline passed as the method argument. Because ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found