-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Comments
fix:
|
IslandRhythms
added
the
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
label
Jul 19, 2024
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
This was referenced Aug 21, 2024
This was referenced Aug 21, 2024
This was referenced Sep 14, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
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:
in the cast$expr.js file, when the presence of $switch is checked, there is an issue with the branches and default check :
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
throw the error, but using te collection it does not
Expected Behavior
Should work as it does when using the collection instead of te mongoose model
The text was updated successfully, but these errors were encountered: