Mongoose查询与undefined

我正在尝试使用mongoose构build一个查询相当于这个SQL:

select * from problems where tutorialNumber is not null 

我试过了:

  var q = Problem.find().where('tutorialNumber').ne(undefined); q.exec(callback); 

它返回一个错误:CastError:强制转换为string失败,path为“tutorialNumber”,值为“undefined”

什么是正确的方法来做到这一点?

感谢您的答复。 我find了另一种方法来做到这一点:

 var q = Problem.find().exists('tutorialNumber', true); q.exec(callback); 

有几个语法选项。 我相信你的代码是确定的,而不是你应该使用null而不是undefined 。 我比较喜欢正常的mongoshell的样式:

 Problem.find({tutorialNumber: {$ne: null}}, callback); 

或者你可以做

 Problem.find().ne('tutorialNumber', null).exec(callback); 

但是我相信你使用wherene方式也是正确的。

但是, CastError可能意味着您的模式存在问题(可能试图嵌套模型而不是嵌套模式)。