简体   繁体   English

在两个集合之间传递数据-Meteor JS

[英]Passing data between two collections - Meteor JS

Example

I have two collections, one for Posts and one for Labels that look like this: 我有两个集合,一个用于发布,一个用于标签,如下所示:

Post {
   "_id": "WZTEGgknysdfXcQBi",
   "title": "ASD",
   "labels": {},
   "author": "TMviRL8otm3ZsddSt",
   "createdAt": "2016-01-14T08:42:42.343Z",
   "date": "2016-01-14T08:42:42.343Z"
}

Label {
   "_id": "9NCNPGH8F5MWNzjkA",
   "color": "#333",
   "name": "Grey Label",
   "author": "TMviRL8otm3ZsddSt"
}

What I want to achieve is to have Posts with multiple Labels. 我要实现的是让帖子带有多个标签。

Problem is I cannot insert label data into the post. 问题是我无法在帖子中插入标签数据。 I have a template to add new post and in there I am repeating over the labels. 我有一个模板来添加新帖子,并且在其中重复标签。 Then in the helpers I check which label is checked and store it into an array, but I cannot insert that array in the Posts collection. 然后在帮助器中,我检查了选中的标签并将其存储到数组中,但是无法将该数组插入Posts集合中。

'submit .add-entry': function(event) {
    var title = event.target.title.value;
    var description = event.target.description.value;

    var checkedLabels = $('.label-checkbox:checked');

    //initiate empty array
    var labelsArray = [];

    //go over the checked labels
    for(i = 0; i < checkedLabels.length; i++){
        var label = checkedLabels[i].value;

        // store ids into array
        labelsArray.push(label)
    };

    Posts.insert({
        title: title,
        description: description,
        labels: labelsArray
    });

Q1: Should I insert all the tags data or only the ID fetch more details from the Tags collection based on that ID? 问题1:我应该插入所有标签数据还是仅插入ID,然后根据该ID从“标签”集合中获取更多详细信息?

Q2: How can I insert that array of labels into a Post? Q2:如何将标签数组插入帖子中? The code above does not work because it needs an Object 上面的代码不起作用,因为它需要一个对象

Q3 What is the best way to achieve such relationship between collections? Q3在馆藏之间建立这种关系的最佳方法是什么?

(Q1) You can insert all tag IDs to that labels list and keep that updated (Q3) I think that's a good practice; (Q1)您可以将所有标签ID插入该labels列表,并保持更新状态(Q3),我认为这是一个好习惯; in your development process try to publish using composite collections with this package: https://github.com/englue/meteor-publish-composite 在您的开发过程中,尝试使用此包的复合集合进行发布: https : //github.com/englue/meteor-publish-composite

(Q2) Inserting is easy: (Q2)插入很容易:

Post.insert({title:'Abc', labels:['one', 'two']});

You would need to use $addToSet to update. 您将需要使用$addToSet进行更新。 Here's an example of a possible method: 这是一个可能的方法的示例:

let setModifier = {
  $addToSet: {
    labels: myArrayOfLabelIDs
  }
};
return Post.update({
  _id: id
}, setModifier);

Note : Never use inserts on the client (like you did). 注意 :切勿在客户端上使用插入(就像您一样)。 My example can be added as a method on the server (use check() inside that) and can be called from the client upon submit like: 我的示例可以作为方法添加到服务器上(在其中使用check() ,并且可以在提交后从客户端调用,例如:

Meteor.call('myMethod', id, labelsArray)

Q1: Should I insert all the tags data or only the ID fetch more details from the Tags collection based on that ID? 问题1:我应该插入所有标签数据还是仅插入ID,然后根据该ID从“标签”集合中获取更多详细信息?

It depends: does the label change? 这取决于:标签是否会更改? If it mutable, then storing the ID only is best so that fetching the Label by ID will always return the proper data. 如果它可变,则最好仅存储ID,以便按ID提取Label将始终返回正确的数据。 Otherwise you need to update every Post with the new label info. 否则,您需要使用新标签信息更新每个帖子。

Q2: How can I insert that array of labels into a Post? Q2:如何将标签数组插入帖子中? The code above does not work because it needs an Object 上面的代码不起作用,因为它需要一个对象

If the code does not work, it is likely that you are using some sort of schema that is not matched: to insert an array in place of an object, you need to modify the schema to accept an Array. 如果代码不起作用,则可能是您使用了某种不匹配的架构:要插入数组代替对象,您需要修改架构以接受Array。 You did not post details of a schema. 您没有发布架构的详细信息。

Q3 What is the best way to achieve such relationship between collections? Q3在馆藏之间建立这种关系的最佳方法是什么?

You should look at 'denormalization': this is the term used for this type of things in a NoSQL database like MongoDB. 您应该查看“非规范化”:这是在NoSQL数据库(例如MongoDB)中用于此类事物的术语。 There is no straight answer, it depends on the relationship (1-1, 1-many, many-1) and the ratio of each 'many': in some cases it is best to nest data into objects if they are immutable or if dealing with updates is not too much of a pain. 没有直接的答案,这取决于关系(1-1、1-许多,many-1)以及每个“许多”的比率:在某些情况下,如果数据是不可变的或如果它们不可变,则最好将数据嵌套到对象中处理更新不是很麻烦。

this series of blog posts should help you get a better understanding: http://blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part-1 这一系列博客文章应有助于您更好地理解: http : //blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM