LearnBoost/mongoose — Issue #591: $where inside $elemMatch issue "TypeError: Cannot call method 'path' of undefined"
Author: jleibund
Created: 2011-11-04 02:07:35+00:00
State: open

Mongoose 2.3.8: The following query works in mongo from the cmd line - its a geo query w/date-based expression on array of sub-objects:

db.things.find({ schedule2:
{ '$elemMatch':
{ recur: 'weekly',
'$where': 'new Date( ((3+this.startDay%6)*86400000) + 3600000*this.startTime + 1319673600000 ).getTime() <= 1320343200000 && new Date( ((3+this.stopDay%6)*86400000) + 3600000*this.stopTime + 1319673600000 ).getTime() >= 1320325200000' } },
area: { '$in': [ '20001' ] },
curr_location:
{ '$near': [ -77.014397, 38.916083 ],
'$maxDistance': 0.2898550724637681 } });

The equivalent in mongoose produces:

TypeError: Cannot call method 'path' of undefined
at Query.cast (/project/thing/node_modules/mongoose/lib/query.js:175:27)
...

Maybe embedding $where expressions inside the $elemMatch is the issue? - that block of query.js may be trying to interpret that $where as a path on the model.

that block of code:

var where = {};
where.schedule2 = { $elemMatch: { recur:'weekly',
$where: 'new Date( ((3+this.startDay%6)*86400000) + 3600000*this.startTime + '+startThurs+' ).getTime() <= '+stopDate.getTime() +
' && new Date( ((3+this.stopDay%6)*86400000) + 3600000*this.stopTime + '+stopThurs+' ).getTime() >= '+startDate.getTime()
}};
// ] };
where.area = {$in: [zip]};
where.curr_location = { $near : [lon-0,lat-0] , $maxDistance : dist-0 };

this.find(where).populate('comp').skip(f).limit(l).run(callback);