【问题标题】:Meteor collections: how to connect to different collections referring to each other?流星集合:如何连接到相互引用的不同集合?
【发布时间】:2015-08-31 11:41:52
【问题描述】:

我有两个收藏:

Contracts = new Mongo.Collection('contracts');
Reminders = new Mongo.Collection('reminders');

这些在数据库中的结构或多或少是这样的:

合同:

{
  "id": "4432234",
  "contract": "C-42432432",
  "description": "Description of contract",
  "counterpart": "Company name",
  "status": "awarded"
},
etc.

提醒:

{
  "name": "Contract expiring",
  "type": "expiring",
  "contract": "C-42432432",
  "reminderDate": "2015-06-01",
  "urgency": 3
},
etc.

这里的“contract”-name 是指contract-collection 中的“contract”-name。我们可以将多个提醒连接到同一个合同。因此,我希望它们在两个不同的集合中。

要获取我使用的合同数据:

<template name="example">
{{#each contracts}}
    {{description}}
{{/each}}
</template>

对应的js:

Template.example.helpers({
  contracts: function() {
    return Contracts.find();
  }
});

这没问题,在这个实例中的结果是Description of contract。

但是如果我想显示reminder-collection并从Contract-collection中获取相应的数据怎么办?换句话说:我想循环提醒集合并获得相同的输出。

<template name="notworking">
{{#each reminder}}
    {{description}}
    <!-- Here I want the description from the Contract-collection -->
{{/each}}
</template>

对应的js:

Template.notworking.helpers({
  reminder: function() {
    //?
  }
});

【问题讨论】:

    标签: meteor meteor-helper meteor-collections


    【解决方案1】:

    https://atmospherejs.com/reywood/publish-composite

    这个包提供了一种灵活的方式来使用响应式连接从各种集合中发布一组相关文档。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您最好使用Contracts._id 来引用Reminders 集合中的合同,这样如果合同名称和描述在某些时候发生更改,您就不需要更新所有相关的提醒。

      合同:

      {
        "_id": "tPN5jopkzLDbGypBu",
        "contract": "C-42432432",
        "description": "Description of contract",
        "counterpart": "Company name",
        "status": "awarded"
      },
      

      提醒:

      {
        "name": "Contract expiring",
        "type": "expiring",
        "contractId": "tPN5jopkzLDbGypBu",
        "reminderDate": "2015-06-01",
        "urgency": 3
      },
      

      那么,如果您想列出提醒并显示相关的合同信息,您将拥有:

      HTML:

      <template name="remindersList>
      {{#each reminder}}
        Date: {{reminderDate}} Urgency: {{urgency}}
        {{#with relatedContract}}
          Description: {{description}} Counterpart: {{counterpart}} Status: {{status}}
        {{/with}}
      {{/each}}
      </template>
      

      JS:

      Template.remindersList.helpers({
        reminder: function(){
          return Reminders.find(); // or whatever query you need to run
        },
        relatedContract: function(){
          return Contracts.findOne({_id: this.contractId}); // 'this' will be the Reminder document
        }
      });
      

      或者 - 如果你想保留你的非规范化架构,那么 relatedContract 函数只需要返回 Contracts.findOne({contract: this.contract})

      【讨论】:

        【解决方案3】:

        我假设您需要遍历循环内的每个提醒,遍历每个合同:

        <template name="contracts">
          {{#each contracts}}
            <p>{{description}}</p>
            {{#each reminders}}
              <p>{{description}}</p>
            {{/each}}
          {{/each}}
        </template>
        

        评估reminders时的当前数据上下文将是当前迭代的合约,因此我们可以使用this.contract访问其合约名称。

        因此,您只需返回每个具有相同合同名称的提醒文档。

        Template.contracts.helpers({
          reminders: function(){
            return Reminders.find({
              contract: this.contract
            });
          }
        });
        

        【讨论】:

          猜你喜欢
          • 2014-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多