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

$Reduce $Map Returning incorrect results and making other fields return undefined. #289

Closed
gak10100 opened this issue Dec 24, 2022 · 0 comments
Labels

Comments

@gak10100
Copy link

gak10100 commented Dec 24, 2022

import { aggregate } from "mingo";
import "mingo/init/system";

function main() {
  const result = aggregate(
    [{
      "skills":{
        "data": [
          {
            "name":"Respiratory Therapst",
            "categories":{
              "data":[
                {
                  "name": "Healthcare"
                }
              ]
            }
          },
          {
            "name":"Phlebotomy",
            "categories":{
              "data":[
                {
                  "name": "Skills (N-R) - DNU for Postings"
                },
                {
                  "name": "Healthcare"
                }
              ]
            }
          }
        ]
      },
      "location" : 'abcd'
  }],
  [
    {
      "$project" : {
        "specialties" : {
            "$reduce" : {
              "input" : "$skills.data",
              "initialValue" : [],
              "in" : {
                "$concatArrays" : [
                  "$$value",
                  {
                    "$map" : {
                      "input" : "$$this.categories.data",
                      "as" : "category",
                      "in" : {
                        "skill" : "$$this.name",
                        "category" : "$$category.name"
                      }
                    }
                  }
                ]
              }
            }
          },
          "location": "$location"
        }
      }
  ]);

  console.log(result[0]);
}

main();

Expected Result

{
    "specialties" : [
        {
            "skill" : "Respiratory Therapst",
            "category" : "Healthcare"
        },
        {
            "skill" : "Phlebotomy",
            "category" : "Skills (N-R) - DNU for Postings"
        },
        {
            "skill" : "Phlebotomy",
            "category" : "Healthcare"
        }
    ],
    "location" : "abcd"
}

Actual Result

{
  specialties: [
    { skill: 'Healthcare', category: 'Healthcare' },
    {
      skill: 'Skills (N-R) - DNU for Postings',
      category: 'Skills (N-R) - DNU for Postings'
    },
    { skill: 'Healthcare', category: 'Healthcare' }
  ]
}

When using $map inside a $reduce it is returning incorrect results and causing fields projected after the reduce map to be undefined.

@kofrasa kofrasa added the bug label Dec 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants