I am trying to use mongoose to construct a query equivalent to this SQL:
select * from problems where tutorialNumber is not null
I've tried:
var q = Problem.find().where('tutorialNumber').ne(undefined);
q.exec(callback);
It returned an error: CastError: Cast to string failed for value "undefined" at path "tutorialNumber"
What is the right way to do that?
There are several syntax options. I believe your code is OK other than you should use null instead of undefined. I prefer the style that is a little closer to normal mongo shell:
Problem.find({tutorialNumber: {$ne: null}}, callback);
or you can do
Problem.find().ne('tutorialNumber', null).exec(callback);
But I believe the way you are using where and ne are also correct.
However, the CastError can mean there's a problem in your schema (could be trying to nest models instead of nesting schema).
Thanks for the replies. I found another way to do it:
var q = Problem.find().exists('tutorialNumber', true);
q.exec(callback);