【问题标题】:fetch data from a collection by checking a condition in another collection通过检查另一个集合中的条件从集合中获取数据
【发布时间】:2016-03-17 13:52:13
【问题描述】:

我正在研究角度流星。 在我的数据库中有两个集合,即订阅和学校。 订阅:

[
{
    "_id" : "9qAcc86yhG7hSxejL",

    "school" : {
        "_id" : "uz7rMGXJwn566GhBg"
    },

 "wrk_status":"completed"


},
{
    "_id" : "9qAcc86yhG7hSxejL",

    "school" : {
        "_id" : "uz7oMGXJwn566GhBg"
    },

 "wrk_status":"pending"


},
{
    "_id" : "9qAcc86yhG7hSxejL",

    "school" : {
        "_id" : "uz7pMGXJwn566GhBg"
    },

 "wrk_status":"completed"


}]

学校:

[
{
    "_id" : "uz7rMGXJwn566GhBg",

    "name" : "The Oxford School",
    "address" : "Trivandrum",

    "status" : "new",

    },
{
    "_id" : "uz7oMGXJwn566GhBg",

    "name" : "The  School",
    "address" : "Trivandrum",

    "status" : "new",

    },
{
    "_id" : "uz7pMGXJwn566GhBg",

    "name" : "The new School",
    "address" : "Trivandrum",

    "status" : "new",

    }]

所以我想从订阅集合中的 wrk_status 完成的学校集合中获取学校的名称、地址和状态。 所以一开始我想订阅订阅集合并获取wrk_status完成的学校._id,然后订阅学校集合以列出具有该学校的学校的详细信息._id

【问题讨论】:

  • 您可能会后悔为集合命名subcriptions...

标签: angularjs meteor angular-meteor


【解决方案1】:

您可以为此使用 reywood:publish-composite 包。

提供了一种灵活的方式来使用反应式连接从各种集合中发布一组相关文档。这使得一次发布整个文档树变得容易。发布的集合是反应性的,并且会在添加/更改/删除时更新。

这里是link

【讨论】:

    【解决方案2】:

    制作学校词典

    var sch_dict = {};
    shools.forEach(function(sch) {
        sch_dict[sch._id] = sch
    });
    
    var result = []
    subscriptions.forEach(function(sub) {
        if (sub.wrk_status = 'completed') {
           result.push(sch_id[sub.school._id]);
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2020-06-06
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多