简体   繁体   中英

Complex collection.find() query and render it using {{#each collection}}

I have a collection "Members" in MongoDB with this initial data:

        Members.insert({
            event: 1,
            username: "Juan Machado",
            answers: ["Tennis", "Golf", “Bowling”]
        });

        Members.insert({
            event: 1,
            username: "Elvis Crespo",
            answers: [“FootBall”, “Tennis”, “Squash”]
        });

        Members.insert({
            event: 1,
            username: "Roman Santos",
            answers: [“Golf”, “Bowling”]
        });

If the logged in user is the first inserted (“Juan Machado”), I want to show him the usernames of other members who match in the same sports, showing in my rendered template something like that:


“Juan Machado” this are your matches:

  • “Roman Santos” Match with you in: 1. Golf | 2. Bowling
  • “Elvis Crespo” Match with you in: 1. Tennis

Any help how to doing it in Meteor using {{#Each Members}} ? I have difficulties to do the collection.find({}) query and also in rendering it in my template.

First, save the user in the mebers collection by id not by username because usernames change and ids do not. Next for the sports I suggest an array of sports. Then by using this query you could check if an element is in the array: {"answers": {$in: [element]}} . First find the current user in the helper by using this._id . Then pull a doc from the base by his is (or username as you do now). And then obtain his array of answers and use a foreach loop to go through them. The rest is for you to decide how to do it.

Remark when you do each in the HTML, you do it for members collection and in this scope you call the helper.

First let's deal with finding the matching members. You're going to want to publish this from the server with:

Meteor.publish('matchingMembers',function(){
  let username = Meteor.users.find({ _id: this.userId }).username;
  if ( username ){
    let myAnswers = Members.find({ username: username }).answers;
    if ( myAnswers ){
      return Members.find({ answers: { $in: myAnswers }});
    }
  }
});

In your template, assuming you've subscribed to the publication and that it is ready, you'll want:

{{#each otherMembers}}
  {{username}} matches with you in:
  {{#each matchingAnswers}}
    {{this}}
  {{/each}}
{{/each}}

You'll need the following helpers:

Template.myTemplate.helpers({
  otherMembers: function(){
    let myUsername = Meteor.user().username;
    return Members.find({ username: { $ne: myUsername }});
  },
  matchingAnswers: function(){
    let myAnswers = Members.findOne({ username: Meteor.user().username }).answers;
    // return the intersection of the other member's answers with the current user
    return _.intersection(this.answers,myAnswers); /
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM