猫鼬节点。js,查询$lt和$gt不工作。

时间:2021-01-07 00:13:43

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).

我想要所有的学生,他们的最后分数在15到20之间。为此,我在mongoDB中使用mongoose执行以下查询:模型运行良好(所有其他查询都没问题)。

Pupils.find({"marks[-1].value": {'$lt' : 20 }, "marks[-1].value" : { '$gt' : 15 }}, function(err, things){

This is not working, is there something I missed ?

这没用,我漏掉了什么吗?

* UPDATE *

*更新*

I found something like:

我发现了类似:

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 ?

但这也不管用。在这种情况下,是否有办法得到标记数组的最后标记?

3 个解决方案

#1


4  

Lets consider your Pupils collection:

让我们来看看你的学生收藏:

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

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}});

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

您也可以使用$where查询最新标记,但要小心,因为:

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

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

#2


0  

I believe it's not working because the embedded collections in mongo are accessed like this: "marks.0.value", although I haven't used mongoose.

我认为它不起作用,因为mongo中的嵌入集合是这样访问的:“marks.0。值",虽然我没有用过猫鼬。

Unfortunately for your scenario, I do not think there is a way to use negative indexing. (Mongo doesn't guarantee a preserved natural order unless you use a capped collection anyway though)

不幸的是,对于您的场景,我认为没有办法使用负索引。(Mongo并不能保证自然秩序得到保护,除非你使用带帽的藏品)

You may be able to accomplish this using Map/Reduce or a group command

您可以使用Map/Reduce或组命令来完成此任务。

http://www.mongodb.org/display/DOCS/MapReduce

http://www.mongodb.org/display/DOCS/MapReduce

#3


-1  

This is tip than an answer.

这是小费而不是答案。

Use double quotes for special key words like $elemMatch, $get, $lt etc while using mangoose.

在使用mangoose的时候,对特殊的关键词使用双引号,比如$elemMatch, $get, $lt等等。

In following code,$gt will not work properly.

在以下代码中,$gt将不能正常工作。

    var elmatch = { companyname: mycompany };
    var condition = { company_info: { $elemMatch: elmatch } };

    if(isValid(last_id)){
        condition._id = { $gt: new ObjectId(last_id) };
    }
    console.log(condition);

    User.find(condition).limit(limit).sort({_id: 1}).exec(function (err, lists) {
        if (!err) {
            res.send(lists);
            res.end();
        }else{
            res.send(err);
            res.end();

        }
    });

But this issue is solved when i'm using double quotes for special keywords

但是当我对特殊关键字使用双引号时,这个问题就解决了

    var elmatch = { companyname: mycompany };
    var condition = { company_info: { "$elemMatch": elmatch } };

    if(isValid(last_id)){
        condition._id = { "$gt": new ObjectId(last_id) };
    }
    console.log(condition);

    User.find(condition).limit(limit).sort({_id: 1}).exec(function (err, lists) {
        if (!err) {
            res.send(lists);
            res.end();
        }else{
            res.send(err);
            res.end();

        }
    });

I hope it will be helpful.

我希望这能有所帮助。

#1


4  

Lets consider your Pupils collection:

让我们来看看你的学生收藏:

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

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}});

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

您也可以使用$where查询最新标记,但要小心,因为:

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

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

#2


0  

I believe it's not working because the embedded collections in mongo are accessed like this: "marks.0.value", although I haven't used mongoose.

我认为它不起作用,因为mongo中的嵌入集合是这样访问的:“marks.0。值",虽然我没有用过猫鼬。

Unfortunately for your scenario, I do not think there is a way to use negative indexing. (Mongo doesn't guarantee a preserved natural order unless you use a capped collection anyway though)

不幸的是,对于您的场景,我认为没有办法使用负索引。(Mongo并不能保证自然秩序得到保护,除非你使用带帽的藏品)

You may be able to accomplish this using Map/Reduce or a group command

您可以使用Map/Reduce或组命令来完成此任务。

http://www.mongodb.org/display/DOCS/MapReduce

http://www.mongodb.org/display/DOCS/MapReduce

#3


-1  

This is tip than an answer.

这是小费而不是答案。

Use double quotes for special key words like $elemMatch, $get, $lt etc while using mangoose.

在使用mangoose的时候,对特殊的关键词使用双引号,比如$elemMatch, $get, $lt等等。

In following code,$gt will not work properly.

在以下代码中,$gt将不能正常工作。

    var elmatch = { companyname: mycompany };
    var condition = { company_info: { $elemMatch: elmatch } };

    if(isValid(last_id)){
        condition._id = { $gt: new ObjectId(last_id) };
    }
    console.log(condition);

    User.find(condition).limit(limit).sort({_id: 1}).exec(function (err, lists) {
        if (!err) {
            res.send(lists);
            res.end();
        }else{
            res.send(err);
            res.end();

        }
    });

But this issue is solved when i'm using double quotes for special keywords

但是当我对特殊关键字使用双引号时,这个问题就解决了

    var elmatch = { companyname: mycompany };
    var condition = { company_info: { "$elemMatch": elmatch } };

    if(isValid(last_id)){
        condition._id = { "$gt": new ObjectId(last_id) };
    }
    console.log(condition);

    User.find(condition).limit(limit).sort({_id: 1}).exec(function (err, lists) {
        if (!err) {
            res.send(lists);
            res.end();
        }else{
            res.send(err);
            res.end();

        }
    });

I hope it will be helpful.

我希望这能有所帮助。