简体   繁体   English

meteorjs和mongodb:如何将多个集合分组为一个

[英]meteorjs and mongodb: how to group many collections into one

My application is a list of products. 我的应用程序是产品列表。

I have too many, and want to group them, so I can refactor my publish/subscribe code (and my templates as well). 我人数太多了,想对它们进行分组,因此我可以重构我的publish/subscribe代码(以及我的模板)。

Assume 4 distinct collections in my mongodb: balloons, balls, tents, tea. 假设我的mongodb中有4个不同的集合: balloons, balls, tents, tea.

I need to group them into newly created foo and bar . 我需要将它们分为新创建的foobar Then I can write two publish/subscribe statements instead of 4, and access my data by doing something like: 然后,我可以编写两个而不是4的publish / subscribe语句,并通过执行以下操作来访问我的数据:

on Client: 在客户端上:

Foo = new Meteor.Collection('foo');
Bar = new Meteor.Collection('bar');

in html template 在html模板中

{{#each foo.balloons }}

    <p>{{ size }}</p>  
    <p>{{ price }}</p>

{{/each}}

or in another html template 或在另一个HTML模板中

{{#each bar.tents }}

    <p>{{ size }}</p>  
    <p>{{ price }}</p>

{{/each}}

Personally I would not group them into multiple collections. 我个人不会将它们分为多个集合。 I would add a variable, such as "group" with the value "balloons", "balls", "tents" or "tea". 我将添加一个变量,例如“ group”,其值为“ balloons”,“ balls”,“ tents”或“ tea”。

When you then subscribe, you can opt to subscribe to one or more groups at the same time. 然后,当您订阅时,您可以选择同时订阅一个或多个组。 Then in your helpers, simply do something like: 然后在您的助手中,只需执行以下操作:

Template.foo.helpers({
    balloons : function() {
        return Foo.find({
            "group" : "balloons"
        });
    },
    tents : function() {
        return Foo.find({
            "group" : "tents"
        });
    }
});

Template.bar.helpers({
    balls : function() {
        return Foo.find({
            "group" : "balls"
        });
    },
    tea : function() {
        return Foo.find({
            "group" : "tea"
        });
    }
});

Update as per request: 根据要求更新:

<body>
    {{> foo}}
    {{> bar}}
</body>

<template name="foo">
    <div id="balloons" class="product-list">
        {{#each balloons}}
            {{> productItem}}
        {{/each}}
    </div>
    <div id="tents" class="product-list">
        {{#each tents}}
            {{> productItem}}
        {{/each}}
    </div>
</template>

<template name="bar">
    <div id="balls" class="product-list">
        {{#each balls}}
            {{> productItem}}
        {{/each}}
    </div>
    <div id="tea" class="product-list">
        {{#each tea}}
            {{> productItem}}
        {{/each}}
    </div>
</template>

<template name="productItem">
    <div class="product">
        <h1>{{title}}</h1>
        <p>{{description}}</p>
    </div>
</template>

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

相关问题 如何查询MongoDB中有“一对多”关系的两个collections? - How to query two collections that have a “one-to-many” relationship in MongoDB? 如何使用 MongoDB 或 Mongoose 在一个查询中对两个集合进行分组 - How to group two collections in one query with MongoDB or Mongoose 使用MeteorJS从MongoDB中的多个集合中删除文档 - Removing document from multiple collections in MongoDB with MeteorJS Winston MongoDB:如何通过管道将日志发送到多个集合,而不是全部发送到一个集合 - Winston MongoDB: How to pipe logs to many collections, instead all goes to one collection 了解MongoDB中的多对多关系以及如何取消引用集合 - Understanding Many-to-Many relationships in MongoDB and how to dereference collections MeteorJS和MongoDB:如何获取下10个条目 - MeteorJS & MongoDB: How to get next 10 entries 如何在MeteorJS中更新MongoDB集合上的子文档数组 - How to Update An Array of Subdocuments on a MongoDB Collection in MeteorJS 如何使用MongoDB在MeteorJs中存储用户操作? - How to store user actions in MeteorJs using MongoDB? 如何将一个文档保存到 mongodb 中同一数据库的两个不同 collections 中 - How to save one document into two different collections of same database in mongodb 如何将文档从一个集合移到另一个集合? Mongoose,MongoDB - How to move document from one collections to another? Mongoose, MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM