Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongodb:最后N条记录,跳过集合大小-30?_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript Mongodb:最后N条记录,跳过集合大小-30?

Javascript Mongodb:最后N条记录,跳过集合大小-30?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,简单: models.Message.find({ chat_id: req.params.chat_id }).skip(80).limit(30).sort({sent:1}).exec(function(err, message) { if(err) { res.json({error: 'Message not found.'}); } else { res.json(message); } }); 如何编写skip()跳过整个集

简单:

models.Message.find({ chat_id: req.params.chat_id }).skip(80).limit(30).sort({sent:1}).exec(function(err, message) {
    if(err) {
        res.json({error: 'Message not found.'});
    } else {
        res.json(message);
    }
});
如何编写
skip()
跳过整个集合减去最后30个,使其成为动态的


详细信息:

models.Message.find({ chat_id: req.params.chat_id }).skip(80).limit(30).sort({sent:1}).exec(function(err, message) {
    if(err) {
        res.json({error: 'Message not found.'});
    } else {
        res.json(message);
    }
});
我之所以需要这样做,是因为它是一个聊天应用程序,消息需要从最早返回到最新,因此
排序({sent:1})
,但收集量可能会越来越大,我只想发送30个文档

比如说

[
    {sent: 1, message:'hey'},
    {sent: 2, message:'sup'},
    {sent: 3, message:'nttn much'}
]
对于这个例子,我想向客户机发送一条消息,静态情况下可能是这样的

models.Message.find({ chat_id: req.params.chat_id }).skip(2).limit(1).sort({sent:1}).exec(function(err, message) {
    if(err) {
        res.json({error: 'Message not found.'});
    } else {
        res.json(message);
    }
});
这将返回最新的
{sent:3,message:'nttn move'}
,这很好

但是如果没有skip方法,它看起来是这样的

models.Message.find({ chat_id: req.params.chat_id }).limit(1).sort({sent:1}).exec(function(err, message) {
    if(err) {
        res.json({error: 'Message not found.'});
    } else {
        res.json(message);
    }
});
将此返回到客户端
{sent:1,message:'hey'}
,不太好

因此,显然我需要跳过,我需要对mongoDB文档进行更多的搜索,但是必须有一种方法来检查集合长度,然后减去
x
,或者在本例中大约30,然后将其传递到
skip()


你知道我该怎么做吗?

你可以使用
跳过(80).限制(30).排序({sent:-1})
来获取最后30个文档,然后在你的客户端应用程序中按你想要的方式排序。

所以我意识到在执行
res.json
并发送到客户端之前,我可以在查询数据库后使用对象。这是我想到的,到目前为止似乎是可行的

getById: function(req, res) {
    if (req.user) {
        models.Message.find({ chat_id: req.params.chat_id }).sort({sent:1}).exec(function(err, message) {
            if(err) {
                res.json({error: 'Message not found.'});
            } else {
                console.log(message.length);
                message = message.slice(message.length - 30);
                console.log(message);
                res.json(message);
            }
        });
    }
},

我喜欢并考虑过这一点,但同时我觉得我想在服务器端处理它,我意识到在查询DB
消息后
是一个对象数组,因此我可以使用vanilla JS从对象数组中删除对象,然后
res.json
一个新的对象数组。现在正在做这件事..可能应该将
消息
重命名为
消息
,但如果有人对上述不理想的原因有任何评论,我洗耳恭听。