i realize that my underscore function is of course working with, and indeed returning, an array rather than the reactive cursor i need. i know one solution would be to simply pluck the message ids and then run another .find on messages, but is there another / better / more efficient / more natural way to return a cursor with the result set i'm looking for?推荐答案这是最终的工作代码.这确实会返回一个游标,因为我所做的基本上是从多个查询/修改中获取结果,并将一组 id 输入到最终的 .find 查询中.Heres the final working code. This does return a cursor as basically what I'm doing is taking the results from multiple queries / modifications and pumping a set of id's into the final .find query.Meteor.publish('unreads', function() { if (!this.userId) { throw new Meteor.Error('denied', 'not-authorized'); } // setup some vars var messageQuery, messages, userGroupQuery, userGroups, uniqeMsgIds; var self = this; var user = Meteor.users.findOne(self.userId); var userIdArr = [self.userId]; // for use where queries require an array var contacts = user.contacts; // get groups userGroupQuery = Groups.find({ $or : [ { owner : self.userId }, { members : self.userId } ] }, { // Projection to only return the _id field fields : { _id:1 } } ); // create an array of group id's that belong to the user. userGroups = _.pluck(userGroupQuery.fetch(), '_id'); messages = Messages.find({ $or : [ { // unread direct messages $and : [ { participant : self.userId }, { userId : { $in : contacts } }, { readBy : { $nin : userIdArr } } ] }, { // unread group messages $and : [ { groupId : { $in : userGroups } }, { readBy : { $nin : userIdArr } } ] }, ] }, { sort : { // put newest messages first time : -1 } }); // returns an array of unique documents based on userId or groupId uniqueMessages = _.uniq(messages.fetch(), function(msg) { if (msg.groupId) { return msg.groupId; } return msg.userId; }); /* Get the id's of all the latest unread messages (one per user or group) */ uniqeMsgIds = _.pluck(uniqueMessages, '_id'); /* finally publish a reactive cursor containing one unread message(latest) for each user/group */ return Messages.find({ _id : { $in : uniqeMsgIds } });}); 这篇关于在 Meteor 中,如何将查找查询的处理结果发布为游标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-17 02:18
查看更多