Javascript 流星收集与用户的关系
我有一些相关的集合,其中一个是Javascript 流星收集与用户的关系,javascript,jquery,mongodb,collections,meteor,Javascript,Jquery,Mongodb,Collections,Meteor,我有一些相关的集合,其中一个是Decisions,它有这个字段 提交人:{ 类型:Schema.user, autoValue:function(){ if(this.isInsert){ 返回this.userId; }否则{ 这是unset(); } } } 因此,当一个登录用户发布一个决策时,这个字段包含他的userId,但是当我使用{{{each}}将数据检索到HTML中时,字段{{{submittedBy}只有它的userId。。。我想在中检索用户的对象以对其进行操作,例如,为文本呈
Decisions
,它有这个字段
提交人:{
类型:Schema.user,
autoValue:function(){
if(this.isInsert){
返回this.userId;
}否则{
这是unset();
}
}
}
因此,当一个登录用户发布一个决策
时,这个字段包含他的userId,但是当我使用{{{each}}
将数据检索到HTML中时,字段{{{submittedBy}
只有它的userId。。。我想在中检索用户的对象以对其进行操作,例如,为文本呈现username,为
标记呈现userId
扩展用户
具有此字段
决定:{
类型:[Schema.Decision],
可选:true
}
我想用用户发布的每个决策来填充它
我使用的是autoforms
和collection2
如果您想看一下,这里是完整的模式
Decisions = new Meteor.Collection("decisions");
var Schema = {};
Schema.Decision = new SimpleSchema({
title: {
type: String,
label: "Título",
max: 149
},
description: {
type: String,
label: "Descripción",
max: 149,
optional: true
},
blue: {
type: String,
label: "Opción Azul",
max: 149
},
blueTotal: {
type: Number,
label: "Votos Azules",
min: 0,
defaultValue: 0,
optional: true
},
red: {
type: String,
label: "Opción Roja",
max: 149
},
redTotal: {
type: Number,
label: "Votos Rojos",
min: 0,
defaultValue: 0,
optional: true
},
createdAt: {
type: Date,
label: "Fecha envío",
optional: true,
autoValue: function () {
if (this.isInsert) {
return new Date;
} else {
this.unset();
}
},
denyUpdate: true
},
submittedBy: {
type: Schema.user,
autoValue: function() {
if (this.isInsert) {
return this.userId;
} else {
this.unset();
}
}
},
tags: {
type: [String],
label: "Tags",
minCount: 1
}
});
Schema.User = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
username: {
type: String,
regEx: /^[a-z0-9A-Z_]{3,15}$/
},
emails: {
type: [Object]
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
profile: {
type: Schema.UserProfile,
optional: true
},
decisions: {
type: [Schema.Decision],
optional: true
}
});
Schema.UserProfile = new SimpleSchema({
firstName: {
type: String,
regEx: /^[a-zA-Z-]{2,25}$/,
optional: true
},
lastName: {
type: String,
regEx: /^[a-zA-Z]{2,25}$/,
optional: true
},
birthday: {
type: Date,
optional: true
},
gender: {
type: String,
allowedValues: ['Male', 'Female'],
optional: true
},
website: {
type: String,
regEx: SimpleSchema.RegEx.Url,
optional: true
},
bio: {
type: String,
optional: true
},
country: {
type: Schema.UserCountry,
optional: true
}
});
Schema.UserCountry = new SimpleSchema({
name: {
type: String
},
code: {
type: String,
regEx: /^[A-Z]{2}$/
}
});
Decisions.allow({
insert: function() {
return true;
},
update: function() {
return false;
},
remove: function() {
return false;
},
fetch: []
});
Decisions.attachSchema(Schema.Decision);
Meteor.users.attachSchema(Schema.User);
一般来说,不复制数据库中的数据是一个很好的做法。所以,如果您已经将决策
作为一个集合,则不应该将它们存储为用户模型中的数组。另外,如果decision
有一个指向users
集合的用户id,那么user
模型不应该有一个指向另一个方向的decision
id数组。你想打破这条规则有什么好的理由吗?只是我来自SQL模型,从没有SQL开始,现在我几乎不明白我可以得到用户的决定——从决定中获得用户ID,然后用submittedBy搜索用户数据,对吗?我只是觉得它比一对一的关系“慢”…对于大多数用例来说,它并没有明显的慢,假设你在数据库中有适当的索引。减少冗余更为重要,因为它可以防止随着应用程序的增长而出现数据完整性问题–请参阅。