gpt4 book ai didi

javascript - Meteor:如何在通过 id 链接的两个集合之间创建多对多关系?

转载 作者:行者123 更新时间:2023-12-03 04:30:03 25 4
gpt4 key购买 nike

我有两个收藏。一个称为帖子,另一个称为类别。在帖子集合中,是具有 ID 的各个帖子。如图所示,该特定帖子的 id: 1291, 是一个整数,而不是字符串。

enter image description here

第二个集合是类别集合,其中包含每个帖子所属的类别,最重要的是,在每个类别中类别集合是属于该类别的帖子数组。注意posts.ID,本例中为ID:1291,此ID是帖子中的帖子id强>收藏

夏天。

  • 有一个帖子集合,其中包含单个帖子。那些各个帖子属于不同的类别。
  • 还有类别集合,其中包含各个类别。每个类别中都有一个数组,其中包含属于该类别的帖子该类别,但我可以利用的帖子对象很少。

enter image description here

问题

我的流程是这样的。

用户滚动浏览类别列表,然后单击“类别”集合中的一个类别,目的是查看该类别中的帖子。

<template name="category">
{{#each categories}}
<span class="title"><a href="/category/{{_id}}">{{name}}</a></span>
{{/each}}
</template>

就目前情况而言,当用户执行此操作时,他们会查看属于 Categories 集合的类别中已有的帖子(记住类别集合中的 posts 数组)像这样

Template.singleCategory.helpers({
categories() {
var id = FlowRouter.getParam('_id');
return Categories.find({
_id: id
}, {
sort: {
timestamp: 1
},
limit: 100
});
}
});

但是类别集合中的这些帖子没有在帖子集合中找到的某些关键对象,而且我想删除类别中的帖子。

目标

我不想使用在 Categories 集合中找到的这些帖子,而是使用在 Posts 集合中找到的帖子,并且我想使用他们共享相似的 Id。因此,当用户单击某个类别时,他们应该看到的帖子应来自 帖子 集合,由 类别 中的帖子 ID 链接集合,与Posts集合中的post id相同。

代码

这是代码。我想说的是:

In this single category being viewed by the user after they clicked it from the list of other categories, do not show the posts embedded in the Categories collection, Instead, return the Posts collection. In this Posts collection, find each post id, then match it to ID from category.posts within the Categories collection, then Show the objects from Posts collection, not category.posts.

  Template.singleCategory.helpers({
posts(){
return Posts.find({ ID: {$in: this.posts }});
}
});

但是,我似乎无法让它工作。我收到错误

Exception in template helper: Error: $in needs an array

类别集合中的帖子是一个数组。

我该如何解决这个问题?

已编辑

基于答案部分中的代码,我必须处理category.posts,使其成为一个数组。我得到了我想要的结果,在某种程度上,有些帖子没有通过。我想知道,这段代码可以更好吗?

Template.Category.helpers({
categories(){
var id = FlowRouter.getParam('_id');
//create a variable with the correct category by id
var category = Category.findOne(id, {
sort: {
timestamp: 1
},
limit: 100
});
console.log(category);
//target ID within posts
var postArray = category.posts;
console.log(postArray);
//treat the object, clean it up to prepare it for becoming an array
var postArrayStr = JSON.stringify(postArray).replace(/ID/g, '').replace(/\[|\]/g, '').replace(/""/g, '').replace(/:/g, '').replace(/\{|\}/gi, '');
//create an array
var idsArray = postArrayStr.split(',').map(function(item) {
return parseInt(item, 10);
});
//match it to posts id
var matchi = Posts.find({
id: {
$in: idsArray
}
}).fetch();
//
console.log(matchi);
//return it
return matchi;

}

});

最佳答案

我要做的是在类别集合中包含一个仅包含 post mongo 的 _id 字段(这是唯一的)的数组。

然后在帮助程序中,您可以查询帖子集合以返回具有这些 ID 的所有帖子。

所以它会是这样的:

Template.singleCategory.helpers({
posts(){
var id = FlowRouter.getParam('_id');
var category = Category.findOne(id, {sort: {timestamp: 1},limit: 100 })

var postArray = category.posts
// example --> ["rCBWsKLCAWghBRJg4", "yMDtnDSo43ZDEcqpu", "cQeJntQtvwiHpgdZ9"]

return Posts.find( {_id: {$in: postArray}});
// it will return an array oj objects where each object is a post from the posts collection : [Object, Object, Object]
}
});

有更好的方法可以做到这一点,但我认为这个解决方案可以解决您的问题,而无需改变太多事情

关于javascript - Meteor:如何在通过 id 链接的两个集合之间创建多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43528560/

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