我在Meteor中有一些组和一些项目。
每个项目属于一个组。这些组实际上可以分成两部分,其中一些组属于一个具有更高阶(在层次结构中)的组。
所以层次结构看起来像:
Group 1
Subgroup 1
Subgroup 2
Subgroup 3
Group 2
Subgroup 1
Subgroup 2
Group 3
Supgroup 1
这些项目只能属于一个子群组,而不属于“主要群组”。所以我想我可以创建一个只包含子组的集合,然后有一个字段告诉哪个主要组'这个小组属于。但我还需要存储有关“主要群组”的其他信息。 (例如它的名称,日期范围等),所以更好的解决方案是创建两个集合; MainGroup
和SubGroup
,SubGroupId
中有一个Item
字段。但由于这两种类型的组几乎相同,我认为我可以建立一个模式结构,其中子组是“主要组”的子对象。
您通常如何创建此类应用的数据结构?理想情况下,有一天也可能有嵌套组。
答案 0 :(得分:0)
Since you intend to have nested group one day it doesn't make much sense to have two collections (one for each level) since your then need another collection for each further love you have.
In a traditional Mongo setup you'd include the subgroup inside the document for each main groups. This might be a sensible approach in your case.
An alternative would be to have a single collection of groups, where each group can have a parent ID as a field. If there's no parent ID then you know it's a top level group. This approach is useful if you need to sent just the subgroups down to the client (since Meteor can't return a cursor for a sub-collection you'll always need to send the hold sub-document down).
The best structure therefore depends on your app and how you'll be using the data to create your UI.
Finally, as note by another poster you can use the Simple Schema package to enforce a schema once you've decided on it.
答案 1 :(得分:-2)
您需要的是像https://github.com/aldeed/meteor-simple-schema
这样的架构包简单模式
AddressSchema = new SimpleSchema({
street: {
type: String,
max: 100
},
city: {
type: String,
max: 50
},
state: {
type: String,
regEx: /^A[LKSZRAEP]|C[AOT]$/
},
zip: {
type: String,
regEx: /^[0-9]{5}$/
}
});
复合模式
CustomerSchema = new SimpleSchema({
billingAddress: {
type: AddressSchema
},
shippingAddresses: {
type: [AddressSchema],
minCount: 1
}
});
验证对象
obj = {title: "Ulysses", author: "James Joyce"};
isValid = BookSchema.namedContext("myContext").validate(obj);