我想创建这个函数,这样当登录的用户滚动浏览博客文章时,他们可以单击"x"按钮,立即从结果中删除该文章,并且该文章在未来的搜索结果中不会为该特定用户产生结果。
到目前为止,这就是我所拥有的,它的工作原理是通过"x"按钮检索帖子的文档ID。然后我将这个id插入到一个名为…的数组字段中。。。
not_intereted[],
此字段位于用户配置文件中。
Accounts.createUser({
email: template.find("#signup-email").value,
password: template.find("#signup-password").value,
profile: {
not_interested: []} });
每个用户都订阅了整个帖子集。
AllPosts = new Meteor.Collection('allposts')
对于每个渲染后的帖子,都有一个删除按钮,单击后会触发以下事件。此事件成功地将所选文档_id检索到变量"a"中。此变量已成功传递给方法。
Template.postBoard.events({
'click a.close': function(e){
e.preventDefault();
Session.set("selectedPostId", this._id);
var a = Session.get('selectedPostId');
Meteor.call('addToremoveList', a);
}
});
在服务器上:
Meteor.methods({
'addToremoveList': function(a){
Meteor.users.update( { _id: Meteor.userId() }, { $addToSet: { 'profile.not_interested': a }} );
}
});
到目前为止,我所做的一切都奏效了。我可以存储用户希望删除的文档ID。ID存储在名为not_interested
的用户配置文件字段中的一个数组中。
问题:调用`AllPosts.find()时,如何消除这些not_interested
文档;
我不确定如何实现mongos$ne
功能。
谢谢。
您需要使用mongo-nin运算符:http://docs.mongodb.org/manual/reference/operator/query/nin/#op._S_nin
让所有用户订阅整个帖子集合。然后,在客户端上,通过以下操作选择要显示的帖子(可能使用iron路由器的数据功能):
selectedPosts: AllPosts.find({_id: {$nin: Meteor.user().profile.not_interested} })
(我没有测试这个,但它应该可以工作。如果没有,请留下评论。)