How to limit topics returned by a command to most recent 10 instead of all topics ever written?

StackOverflow https://stackoverflow.com/questions/23452282

  •  15-07-2023
  •  | 
  •  

Question

I have a system where users can type in a command and get a return of all topics, sorted by date and time. The topics are growing and it takes a while for all of them to load, how can I limit it so that the user only gets 10 most recent topics instead of all of them?

function finishQuery() {
    query
        .populate('creator editedBy')
        .sort('lastCommentDate')
        .exec(function (err, topics) {
            if (err) return shell.error(err);
            if (!isGui) {
                if (topics.length === 0)
                    shell.warn('No results.');
                else {
                    topics.forEach(function (topic) {
                        var currentUser = shell.getVar('currentUser'),
                            topicView;
                        for (var index = 0; index < topic.views.length; index++) {
                            var view = topic.views[index];
                            if (view.userId == currentUser._id) {
                                topicView = view;
                                break;
                            }
                        }
                        var newCommentCount = topicView ? topic.commentCount - topicView.commentCount : topic.commentCount;
                        shell.log('{{0}} {1}'.format(topic.id, topic.title), {
                            bold: true,
                            dontType: true,
                            cssClass: newCommentCount === 0 && topicView ? 'dim' : ''
                        });
                        shell.log(
                            '{0} by {1}'.format(topic.dateFromNow(), topic.creator.username),
                            { cssClass: 'sub', dontType: true }
                        );
                        shell.log(topic.tags.join(','), { cssClass: 'sub', dontType: true });
                        if (topic.commentCount > 0) {
                            shell.log(
                                '{0} {1} ({2} new)'.format(
                                    topic.commentCount,
                                    topic.commentCount === 1 ? 'Comment' : 'Comments',
                                    newCommentCount
                                ),
                                { cssClass: 'sub', dontType: true }
                            );
                        }
                        shell.log();
                    });
                }
            }
            else {}

        });
}
Was it helpful?

Solution

The file you're querying is most likely using SQL on the server to grab all of the topics. Just find it and add LIMIT 0, 10 to the appropriate query.

P.S. this will return the first 10 in the database, so make sure it's sorting them first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top