【问题标题】:Creating a many-to-many relationship between 2 mongodb collections by ids通过 ids 在 2 个 mongodb 集合之间创建多对多关系
【发布时间】:2016-11-14 02:23:42
【问题描述】:

我有两个系列。一种称为帖子,另一种称为类别。在帖子集合中,每个帖子都包含一个名为 categories 的 ID 数组,这些 ID 是各个帖子所属类别的 ID,存储在帖子中。

第二个集合是 Categories 集合,其中包含每个帖子所属的类别

目标

在我的模板中,我将每个帖子显示为其标题、内容、图像和作者,以及通过将帖子集合中的类别 ID 链接到类别集合中的各个类别而产生的类别名称

<template name="latest">
    {{#each posts}}
<div>{{> category}}</div>
      <h5 class="latest-title">{{title.rendered}}</h5>
  <img class="latest-img" src="{{featured_image_thumbnail_url}}" alt="" />
    {{/each}}
</template>

在我的类别模板中

<template name="category">
  {{#each categories}}
 {{name}}
  {{/each}}

</template>

在我的 category.js 中

Template.category.helpers({
  categories(){
    return CategoryCollection.find({ id: parseInt(this.categories) });
  }
});

如您所见,我想显示属于帖子的类别的名称,它们位于一个数组中,因为帖子可能也有 3 个属于它的类别。但我似乎无法让它工作。

编辑

这是我的编辑,包括$in

Template.category.helpers({
  categories(){
    return CategoryCollection.find({ id: {$in: this.categories }});
  }
});

这是我的模板

<template name="category">
  {{#each categories}}
  {{name}}
  {{/each}}

</template>

它似乎不起作用。

进展

它不起作用,因为我没有为我的示例帖子分配类别,上面编辑的代码就是答案

【问题讨论】:

    标签: javascript mongodb meteor mongo-collection


    【解决方案1】:

    我用于此类内容的另一个解决方案只是显示该帖子数组中的类别,甚至没有帮助。这就是我的工作......

    方法内部:

    //the categories is what you pass from the client as an array of strings
    //IE: ['cat1', 'cat2']
    
    categories.forEach(function(cc){
        const cat = Categories.findOne({name: cc});
        if(!cat){
            Categories.insert({
                name: cc,
                count: 1
            });
        } else {
            //increment count if it exists
            Categories.update({name: cc}, {
                $inc:{
                    count: 1
                }
            });
        }
    });
    

    我插入计数为 1 并在存在类别时增加计数的原因是针对多个不同的用例,例如:

    1. 在搜索时显示现有类别,以便始终返回现有文档

    2. 在编辑/删除帖子的情况下,如果count == 1,则删除类别如果count &gt; 1,则减少该类别的计数。

    3. 在用户添加/编辑帖子时显示对现有类别的引用。根据他们在输入上写的内容,我会返回带有正则表达式的类别推荐。

    在帖子中,只显示帖子中的类别名称。无需查找类别。

    <!-- 
        see that its {{this}} not {{name}} because 
        we saved the categories to the post as an array of strings 
        we'd do {{name}} if we were looping through the categories collection
    -->
    {{#each categories}}
        {{this}}
    {{/each}}
    

    如果您需要像点击事件一样从帖子中的数据库访问类别,您可以快速进行Categories.findOne({name: this.name})

    我看到您将其他内容保存到 Category 集合中,我可能会保存到帖子本身和一些,如果它们像链接等,我什至不会保存和生成客户端所需的东西。

    【讨论】:

    • 这是一种有趣的方式,我将初始代码@Luna 放在我的catego.helper 中吗?
    • @Drakata 你说的初始代码是什么意思?如果您的意思是forEach,那就是服务器中的插入方法。如果您指的是 {{#each categories}} 的助手,则没有。您只需返回文档的类别字段
    【解决方案2】:

    您想使用$in 运算符:

     return CategoryCollection.find({ id: {$in: this.categories }});
    

    【讨论】:

    • 我已将我的编辑放在主要问题的底部。我无法让它工作,我做对了吗? @米歇尔
    • 这实际上是答案,它对我不起作用,因为我使用的帖子本身必须分配给类别。当我分配类别时,它们显示了。再次感谢您
    猜你喜欢
    • 2021-12-21
    • 2017-10-18
    • 2017-11-03
    • 2016-11-01
    • 2017-09-17
    • 2019-08-24
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多