Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$accumulator support (MongoDB 4.4) #9364

Closed
dimaip opened this issue Aug 27, 2020 · 2 comments
Closed

$accumulator support (MongoDB 4.4) #9364

dimaip opened this issue Aug 27, 2020 · 2 comments
Milestone

Comments

@dimaip
Copy link

dimaip commented Aug 27, 2020

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

@vkarpov15 vkarpov15 added this to the 5.10.2 milestone Aug 28, 2020
@vkarpov15
Copy link
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
Copy link
Author

dimaip commented Aug 28, 2020

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants