mongoose node.js,使用 $lt 和 $gt 查询不起作用 [英] mongoose node.js, query with $lt and $gt not working
问题描述
我想获取最后一个分数在 15 到 20 之间的所有学生.为此,我使用 mongoose 在我的 mongoDB 中执行以下查询:模型工作正常(所有其他查询都正常).
I want to get all the pupils whose last mark is between 15 and 20. To do so, I perform the following query in my mongoDB using mongoose: The models are working fine (all the other queries are ok).
Pupils.find({"marks[-1].value": {'$lt' : 20 }, "marks[-1].value" : { '$gt' : 15 }}, function(err, things){
这不起作用,我错过了什么吗?
This is not working, is there something I missed ?
* 更新 *
我发现了类似的东西:
Pupils.find({ "marks[-1].value": {$gt : 15, $lt : 20}});
但这也不起作用.在这种情况下,有没有办法获得标记数组的最后一个标记?
But this does not work either. Is there a way to get the last mark of the marks array in this case ?
推荐答案
让我们考虑一下您的 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
这篇关于mongoose node.js,使用 $lt 和 $gt 查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!