【问题标题】:How to match an integer from one collection with it's identical in another collection, creating a many to many relationship.如何将一个集合中的整数与另一个集合中的相同整数进行匹配,从而创建多对多关系。
【发布时间】:2017-05-17 02:31:24
【问题描述】:

这应该是流星中简单的多对多关系,但我一定错过了一些东西,因为我无法让它工作。

我有一个名为 reblog 的集合,其中有一个名为 descovered 的整数数组,见图

我有第二个名为 posts 的集合,它是一个帖子集合,这些帖子有一个 ID。看第二张图

我想在 postsreblog 集合之间创建多对多关系。即,我想匹配整数

descovered: 9

来自转发集合,带有:

id: 9

来自 posts 集合,这样我就可以只显示与 reblog 集合匹配的帖子。这当然可以让我显示帖子的标题和其他属性。

这是我的 js

Template.reblogging.helpers({
descovered() {
  var id = FlowRouter.getParam('_id');

  //fetch the reblog collection contents

  var rebloged = reblog.find().fetch();

  //log below is showing that the fetch is successful because i can see the objects fetched in console

  console.log(rebloged);

  //create the relationship between the posts collection and the reblog collection

  var reblogger = posts.find({
    id: {
      $in: rebloged
    }
  }).fetch();

  //nothing is showing with the log below, so something is going wrong with the line above?

  console.log(reblogger);
  return reblogger
}
});

我一定是遗漏了一些东西,因为这似乎是一件很简单的事情,但它并不工作

而我的 HTML 是这样的

<template name="reblogging">
 {{#each descovered }}
<ul class="">
  <li>
    <h5 class="">{{title.rendered}}</h5>
  </li>
</ul>
{{/each}}
</template>

【问题讨论】:

    标签: javascript mongodb meteor meteor-collections


    【解决方案1】:

    你不需要转换成字符串和解析,你可以直接在光标上使用.map()来创建一个descovered值的数组。此外,由于您使用的是 Blaze,您可以只返回游标而不是数组。我怀疑您还打算在您的第一个 .find() 中使用您的 FlowRouter _id 参数。如果您不这样做,则无需在帮助程序中获取该参数。

    Template.reblogging.helpers({
      descovered() {
        const id = FlowRouter.getParam('_id');
        const reblogArr = reblog.find(id).map(el => { return el.descovered });    
        return posts.find({ id: { $in: reblogArr } });
      }
    );
    

    【讨论】:

    • 我无法让它工作。 console.log(reblogArr); 什么也没显示,@Michel
    • 什么是reblog.find(id).count()
    【解决方案2】:

    事实证明,匹配是准确的,但是,reblog 集合中的数据需要用 REGEX 处理,以摆脱除我需要的值之外的所有其他内容,然后将它们转换为数组,这是最终有效的代码。把它留在这里,希望它对将来的人有所帮助。

    Template.reblogging.helpers({
    descovered() {
      var id = FlowRouter.getParam('_id');
    
      //fetch the reblog collection contents
    
      var rebloged = reblog.find().fetch();
    
      //log below is showing that the fetch is successful because i can see the objects fetched in console
    
      console.log(rebloged);
    
      //turn it into a string so i can extract only the ids
      var reblogString = JSON.stringify(rebloged).replace(/"(.*?)"/g, '').replace(/:/g, '').replace(/{/g, '').replace(/}/g, '').replace(/,,/g, ',').replace(/^\[,+/g, '').replace(/\]+$/g, '');
      //after  have extracted what i needed, i make it into an array
      var reblogArr = reblogString.split(',').map(function(item) {
        return parseInt(item, 10);
      });
    
      //create the relationship between the posts collection and the reblog collection
    
      var reblogger = posts.find({
        id: {
          $in: reblogArr
        }
      }).fetch();
    
      //nothing is showing with the log below, so something is going wrong with the line above?
    
      console.log(reblogger);
      return reblogger
    }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 2021-09-15
      • 1970-01-01
      • 2021-09-18
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多