基于标准的流域选择

时间:2016-12-13 18:54:37

标签: mongodb meteor

我有一个包含帖子的集合。

帖子文档包含模糊且无模糊的图像(文档中的 blurImage 图像键)。

如果当前用户是帖子的所有者或当前用户已购买内容,则可以将图像密钥推送到客户端。

如果不是这种情况,则无法按下图像键(因为安全性)。否则,任何人都可以查询本地mongo并查看完整的图片网址,而他们并没有为此付费。

现在我的问题是。在发布方法中进行此类字段选择的最佳方法是什么?

我没有看到进行条件字段选择的方法。

任何想法?

2 个答案:

答案 0 :(得分:0)

在您的出版物中:

const user = Meteor.users.findOne(this.userId);
if (user.hasPermissionToViewImage) return Documents.find({}, { fields: image: 1, *everything else you need* });
return Documents.find({}, { fields: image: 0, *everything else you need* });

答案 1 :(得分:0)

这实际上有点棘手,因为需要根据另一个字段(ownerId)对每个图像评估条件。但是,您可以在出版物中返回游标数组,每个游标都有自己的查询条件和字段过滤器。在你的情况下:

Meteor.publish('myPublication',()=>{
  const ownedByMe = Posts.find({ ownerId: this.userId },{ fields: { image: 1, foo: 1, bar: 1 }});
  const notOwnedByMe = Posts.find({ ownerId: { $ne: this.userId }},{ fields: blurredImage: 1, foo: 1, bar: 1 }});
  return [ownedByMe,notOwnedByMe];
});

其中foobar是您要在任何一种情况下返回的键。

@sdybskiy请注意,您不能在一个查询中混合字段包含和排除。包含意味着排除所有其他字段,反之亦然。

相关问题