使用单独集合中的日期对用户进行排序

时间:2016-10-24 01:51:46

标签: mongodb meteor meteor-blaze

我试图根据存储在单独集合中的accountActiveDate来返回用户列表。如果我是console.log candidateUserIDByDate,它会以正确的顺序返回用户,但是当我使用var candidateUserIDByDate返回Meteor.users.find时,它没有排序。

路径:sort.js

let sortCandidatesByDate = CandidateAccountStatus.find({}, {sort: {accountActiveDate: 1}}).fetch();
let candidateUserIDByDate = _.map(sortCandidatesByDate, function(obj) {
  return obj.candidateUserId;
});

return Meteor.users.find({$and: [{_id: { $ne: Meteor.userId() }}, {_id: { $in: candidateUserIDByDate }}]});

1 个答案:

答案 0 :(得分:1)

我认为一个(可能是hackish)解决方案是,返回CandidateAccountStatus并在循环内部,使用另一个帮助器返回正确的用户,如下所示:

模板助手:

status: function(){
    //you might want to do {$ne: {Meteor.userId()}} for the correct field
    //in your selector if you don't want currentUser status
    return CandidateAccountStatus.find({}, {sort: {accountActiveDate: 1}})
},

statusUser: function(){
    //change correctField to how you save the userId to
    //your CandidateAccountStatus schema
    return Meteor.users.findOne({_id: this.correctField})
}

HTML

{{#each status}}
    {{#with statusUser}}
        <!-- You have the user object here -->
        {{_id}} <!-- gives the userId -->
    {{/with}}
{{/each}}