且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mongoose node.js,使用 $lt 和 $gt 查询不起作用

更新时间:2023-11-09 22:29:22

让我们考虑一下您的 Pupils 收藏:

Lets consider your Pupils collection:

Pupils 
{
  _id,
  Marks(integer),
  LatestMark(int)
}

我建议将最新标记添加到 Pupil 文档中(如您在上面的文档中所见),并在每次将新标记添加到嵌套集合时更新它.然后你就可以像这样查询它:

I suggest to add latest mark into Pupil document(as you can see at the document above), and update it each time when you adding new mark into nested collection. Then you will able to query on it like this:

db.Pupils.find({ "LatestMark": {$gt : 15, $lt : 20}});

您也可以使用 $where,但要小心,因为:

Also you can query latest mark using $where, but be care because:

Javascript 执行速度比本地操作符,但非常灵活

Javascript executes more slowly than the native operators, but is very flexible