gpt4 book ai didi

meteor 合集: how to connect to different collections referring to each other?

转载 作者:行者123 更新时间:2023-12-02 04:32:13 24 4
gpt4 key购买 nike

我有两个集合:

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

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

契约(Contract):

{
"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)。因此我希望它们出现在两个不同的集合中。

要获取我使用的契约(Contract)数据:

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

对应的js:

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

这工作正常,此实例的结果是合约描述

但是如果我想显示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() {
//?
}
});

最佳答案

您可能最好使用 Contracts._id 来引用 Reminders 集合中的契约(Contract),这样如果契约(Contract)名称和说明在某个时刻发生更改,您就不会这样做。无需更新所有相关提醒。

契约(Contract):

{
"_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
},

然后,如果您想列出提醒并显示相关的契约(Contract)信息,您可以:

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
}
});

或者 - 如果您想保留非规范化架构,则 latedContract 函数只需返回 Contracts.findOne({contract: this.contract})

关于 meteor 合集: how to connect to different collections referring to each other?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30854295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com