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

$switch in $expr in filter query failed: Cannot read properties of undefined (reading 'map') #14751

Closed
2 tasks done
eherve opened this issue Jul 18, 2024 · 2 comments · Fixed by #14755
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.

Comments

@eherve
Copy link

eherve commented Jul 18, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.6.3

Node.js version

20.11.1

MongoDB server version

7

Typescript version (if applicable)

5.1

Description

when using $switch in filter in query, mongoose throw a type error:

TypeError: Cannot read properties of undefined (reading 'map')
        at _castExpression (node_modules/mongoose/lib/helpers/query/cast$expr.js:96:18)
        at castComparison (node_modules/mongoose/lib/helpers/query/cast$expr.js:213:12)
        at _castExpression (node_modules/mongoose/lib/helpers/query/cast$expr.js:105:18)
        at node_modules/mongoose/lib/helpers/query/cast$expr.js:103:36
        at Array.map (<anonymous>)
        at _castExpression (node_modules/mongoose/lib/helpers/query/cast$expr.js:103:27)
        at cast$expr (node_modules/mongoose/lib/helpers/query/cast$expr.js:75:10)
        at cast (node_modules/mongoose/lib/cast.js:92:13)
        at model.Query.Query.cast (node_modules/mongoose/lib/query.js:4909:12)
        at model.Query.Query._castConditions (node_modules/mongoose/lib/query.js:2232:10)

in the cast$expr.js file, when the presence of $switch is checked, there is an issue with the branches and default check :

  else if (val.$switch != null) {
    val.branches.map(v => _castExpression(v, schema, strictQuery));
    val.default = _castExpression(val.default, schema, strictQuery);
  }

val.$switch is defined but next lines the values used are val.branches and val.default and they are undefined.
Te values should be val.$switch.branches and val.default

Steps to Reproduce

model.find({
  $expr: {
    $eq: [
      {
        $switch: {
          branches: [{case: {$eq: ['$$NOW', '$$NOW']}, then: true}],
          default: false,
        },
      },
      true,
    ],
  },
});

throw the error, but using te collection it does not

model.collection.find({
  $expr: {
    $eq: [
      {
        $switch: {
          branches: [{case: {$eq: ['$$NOW', '$$NOW']}, then: true}],
          default: false,
        },
      },
      true,
    ],
  },
}).toArray();

Expected Behavior

Should work as it does when using the collection instead of te mongoose model

@eherve
Copy link
Author

eherve commented Jul 18, 2024

fix:

} else if (val.$switch != null) {
    val.$switch.branches.map(v => _castExpression(v.case, schema, strictQuery));
    val.$switch.default = _castExpression(val.$switch.default, schema, strictQuery);
}

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 19, 2024
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    name: String,
    scores: [Number]
});

const Test = mongoose.model('Test', testSchema);

async function run() {
    await mongoose.connect('mongodb://localhost:27017');
    await mongoose.connection.dropDatabase();

    await Test.create({
        name: 'Test',
        scores: [87, 86, 78]
    });

    await Test.create({
        name: 'Test',
        scores: [71,64,81]
    });

    await Test.create({
        name: 'Test',
        scores: [91,84,97]
    });


    // docs run
    const test = await Test.aggregate( [
        {
          $project:
            {
              "name" : 1,
              "summary" :
              {
                $switch:
                  {
                    branches: [
                      {
                        case: { $gte : [ { $avg : "$scores" }, 90 ] },
                        then: "Doing great!"
                      },
                      {
                        case: { $and : [ { $gte : [ { $avg : "$scores" }, 80 ] },
                                         { $lt : [ { $avg : "$scores" }, 90 ] } ] },
                        then: "Doing pretty well."
                      },
                      {
                        case: { $lt : [ { $avg : "$scores" }, 80 ] },
                        then: "Needs improvement."
                      }
                    ],
                    default: "No scores found."
                  }
               }
            }
         }
      ] )

    console.log('what is test', test);

    const response = await Test.collection.find({
        $expr: {
            $eq: [
              {
                $switch: {
                  branches: [{case: {$eq: ['$$NOW', '$$NOW']}, then: true}],
                  default: 'Hello There',
                },
              },
              true,
            ],
          },
    }).toArray();

    console.log('what is response', response);

    const simplified = await Test.find({
        $expr: {
          $eq: ['$$NOW', '$$NOW']
        }
      });

      console.log('what is simplifed', simplified);

    // this also throws
    const cleaned = await Test.find({
        $expr: {
          $switch: {
            branches: [
              { case: { $eq: ['$$NOW', '$$NOW'] }, then: true }
            ],
            default: false
          }
        }
      });

      console.log('what is cleaned', cleaned);

    // this throws
    const res = await Test.find({
        $expr: {
            $eq: [
              {
                $switch: {
                  branches: [{case: {$eq: ['$$NOW', '$$NOW']}, then: true}],
                  default: false,
                },
              },
              true,
            ],
          },
    });

    console.log('what is res', res);
}

run();

vkarpov15 added a commit that referenced this issue Jul 23, 2024
fix(query): handle casting $switch in $expr
vkarpov15 added a commit that referenced this issue Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants