【问题标题】:Sorting items from one collection according to values from another collection in根据另一个集合中的值对一个集合中的项目进行排序
【发布时间】:2018-04-22 17:29:01
【问题描述】:

我们正在使用 Meteor 创建一个网站。我有两个 MongoDB 集合。有技能的人:

[{ _id : id1 , name : "computing" } , { _id : id2 , name : "filming" } , ...]

与人合一:

[{ _id : id3 , name : "David" , knows : [...] } , { _id : id4 , name : "Laura" , knows : [...] } , ...]

'knows' 是一个包含 0 到 1 之间值的对象列表,用于描述用户对给定技能的了解程度:

大卫的“知道”:[{ id : id1 , state : 0.81 } , { id : id2 , state : 0.32 }, ...]

我想按状态向用户展示他们的技能。请记住,每个用户的“知识”中不一定会列出每项技能,而只会列出他们已经尝试学习的技能。

我该怎么做?

【问题讨论】:

    标签: mongodb sorting join meteor composite


    【解决方案1】:

    下面是一个帮助您入门的示例:

    html

    <template name="myTemplate">
      {{#each people}}
        <div class="person">
          <div class="person-name">{{name}}></div>
          {{#each skills}}
            <div class="person-skill">{{name}}:{{state}}</div>
          {{/each}}
        </div>
    </template>
    

    js

    Template.myTemplate.helpers({
      people: function() {
        // Maybe Meteor.users.find()?
        return People.find();
      },
      skills: function() {
        // here the context is a person
        return _.chain(this.knows)
          .sortBy('state')  // sort the skills in ascending order
          .map(function(skill) {
            var id = skill.id;
            // join to the skills collection through `id`
            var name = Skills.findOne(id).name;
            return {name: name, state: skill.state};
          });
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多