Skip to content

$function support (MongoDB 4.4) #9897

@eliw00d

Description

@eliw00d

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

What is the current behavior?
I get MongoError: Invalid $addFields :: caused by :: The body function must be specified. unless I add .option({ serializeFunctions: true }), similar to #9364.

Also, I have the object-shorthand eslint rule turned on and it turns:

const documents = await Model.aggregate([
  body: function() {
    // logic
  },

into:

const documents = await Model.aggregate([
  body() {
    // logic
  },

which gives a MongoError: SyntaxError: unexpected token: '{' error.

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

const documents = await Model.aggregate([
    {
      $addFields: {
        newField: {
          $function: {
            body: function(oldField: any) {
              // logic using oldField
            },
            args: ['$oldField'],
            lang: 'js',
          },
        },
      },
    },
  ])

root/tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "skipLibCheck": true
  }
}

root/subfolder/tsconfig.json

{
  "compilerOptions": {
    "lib": ["es2020"],
    "sourceMap": true,
    "strict": true,
    "target": "es2020"
  },
  "extends": "../tsconfig.json"
}

What is the expected behavior?
It should serialize by default, similar to $accumulator.

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

Mongo 4.4.3, Mongoose 5.10.11, Node 14.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions