给定一个过滤和分页的流星出版物,我如何获得应用过滤后的总计数?
客户代码:
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Track } from 'meteor/tracker';
import { Posts } from '/imports/api/posts/posts.js';
import './posts.html';
Template.App_posts.onCreated(function() {
this.subscribe('posts.all', new Date(), 0);
Tracker.autorun(() => {
let postCount = Posts.find({}).count();
console.log(postCount); // 10
});
});
服务器代码:
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { Posts } from '../posts.js';
const postsPerPage = 10;
Meteor.publish('posts.all', function(date, page = 0) {
if (!Meteor.userId()) throw new Meteor.Error('Unauthorised');
check(date, Date);
check(page, Number);
let query = {
userId: Meteor.userId()
};
let options = {};
if (date) {
query.createdAt = date;
}
options.limit = postsPerPage;
options.skip = page * postsPerPage;
let cursor = Posts.find(query, options);
console.log(cursor.count()); // 100
return cursor;
});
这将返回给定日期和页面的预期帖子,但问题是知道总过滤计数。
假设有 1000 个帖子,其中 100 个适用于此日期和用户。当一次只返回 10 个时,如何获得 100 个计数?