光标上的流星投影?

时间:2016-06-03 21:22:13

标签: mongodb meteor

目前,我在我的馆藏上有帮手(使用dburles:collection-helpers)。

例如,在我的Teams集合中,我定义了以下帮助程序。我在客户端上使用了这个助手:

getUnlockedProblems: function() {
    return Problems.find({requirements: {$not: {$elemMatch: {$nin: this.getSolvedIds()}}}});
},

在我的publications.js中,我有一个出版物,用于发布团队解决的问题,这些问题主要使用相同的查询,但过滤掉grader字段除外:

return Problems.find({requirements: {$not: {$elemMatch: {$nin: team.getSolvedIds()}}}}, {fields: {grader: 0}});

有没有办法像return team.getUnlockedProblems().project({fields: {grader: 0}})这样做?所以我可以重复使用查询

1 个答案:

答案 0 :(得分:1)

集合助手是常规函数,因此您可以通过向options添加getUnlockedProblems参数来解决此问题,如下所示:

getUnlockedProblems: function(options) {
  return Problems.find({requirements: ...}, options);
}

然后你可以像这样使用它:

var team = Teams.findOne();

// Get a cursor with a limited set of fields
team.getUnlockedProblems({fields: {grader: 0}});

// Get a cursor with a complete set of fields
team.getUnlockedProblems();