Skip to content

$accumulator support (MongoDB 4.4) #9364

Closed
@dimaip

Description

@dimaip

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

What is the current behavior?

When using the example from the docs on $accumulator I get a weird error $accumulator missing required argument 'init' even though I clearly define init.

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

https://gist.github.com/dimaip/17365161ad6e6615cd50bda4b75ad93c

What is the expected behavior?

It should work as the docs describe

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Mongo 4.4, Mongoose 5.10.0, Node 10.18.1

Activity

added this to the 5.10.2 milestone on Aug 28, 2020
vkarpov15

vkarpov15 commented on Aug 28, 2020

@vkarpov15
Collaborator

Unfortunately, the MongoDB driver doesn't serialize functions by default, which is a problem for $accumulator. You have two options currently:

  1. Use template strings to make your functions into strings:
$accumulator: {
  init: `function() {
    return { count: 0, sum: 0 }
  }`
  // ...
  1. Set the serializeFunctions option:
Model.aggregate([/*...*/]).option({ serializeFunctions: true });

We'll add a fix for this in Mongoose 5.10.2 so Mongoose handles this case automatically 👍

dimaip

dimaip commented on Aug 28, 2020

@dimaip
Author

Thanks so much for the response and the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dimaip@vkarpov15

        Issue actions

          $accumulator support (MongoDB 4.4) · Issue #9364 · Automattic/mongoose