在MongoDB中,Schemas是分开好还是放在一起好?
In MongoDB, is it better to dissociate Schemas or put them together?
以故事为例:一个故事是由许多句子组成的,在我的案例中,故事永远不会超过 20 句话。
最好为故事制作一个模式,为句子制作另一个模式,最后在故事中引用构成故事的句子:
var SentenceSchema = new mongoose.Schema({
// Some other fields ...
sentence: {
type: String,
validate: validateSentence,
trim: true
}
// Some other fields ...
});
var StorySchema = new mongoose.Schema({
// Some other fields ...
//Sentences of the Story
sentences: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Sentence'
}]
// Some other fields ...
});
还是直接把句子放在故事里比较好:
var StorySchema = new mongoose.Schema({
// Some other fields ...
//Sentences of the Story
sentences: [{
// Some other fields ...
sentence: {
type: String,
validate: validateSentence,
trim: true
}
// Some other fields ...
}]
// Some other fields ...
});
对于 20 个句子的故事(最大值),为了获得整个故事,对于第一种情况我们必须进行 20 个连接...效率不高...
但是第二种情况更具有扩展性,例如如果我只想显示一些随机句子,或者更新一个句子...
就我而言,我不认为句子会被重复用于其他故事,故事是逐句写的。我的意思是当一个句子被写下时,它会立即保存在 MongoDB.
提前感谢您的任何建议。
故事中的句子,因为最大值只有 20
MongoDB 是 "joinless" 并且必须用作 "joinless"
只要 "many" 非常大
,就必须为一对多关系使用多个集合
恕我直言,只要故事的大小符合 bson 文档限制,就将它们嵌入到主文档中。
这将加快检索过程,还将启用文档级别的 ACID 更新。
一个句子孤立地有意义吗?你能多次重复使用一个句子吗?我认为如果没有故事的背景,一个句子不太可能有意义,而且你也不太可能想要重复使用一个句子。
因此故事文档应该由句子子文档组成。 IE。您将有一个集合(故事),每个故事文档将包含一组句子子文档。这将是您展示的第二个架构。
以故事为例:一个故事是由许多句子组成的,在我的案例中,故事永远不会超过 20 句话。 最好为故事制作一个模式,为句子制作另一个模式,最后在故事中引用构成故事的句子:
var SentenceSchema = new mongoose.Schema({
// Some other fields ...
sentence: {
type: String,
validate: validateSentence,
trim: true
}
// Some other fields ...
});
var StorySchema = new mongoose.Schema({
// Some other fields ...
//Sentences of the Story
sentences: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Sentence'
}]
// Some other fields ...
});
还是直接把句子放在故事里比较好:
var StorySchema = new mongoose.Schema({
// Some other fields ...
//Sentences of the Story
sentences: [{
// Some other fields ...
sentence: {
type: String,
validate: validateSentence,
trim: true
}
// Some other fields ...
}]
// Some other fields ...
});
对于 20 个句子的故事(最大值),为了获得整个故事,对于第一种情况我们必须进行 20 个连接...效率不高... 但是第二种情况更具有扩展性,例如如果我只想显示一些随机句子,或者更新一个句子...
就我而言,我不认为句子会被重复用于其他故事,故事是逐句写的。我的意思是当一个句子被写下时,它会立即保存在 MongoDB.
提前感谢您的任何建议。
故事中的句子,因为最大值只有 20 MongoDB 是 "joinless" 并且必须用作 "joinless"
只要 "many" 非常大
,就必须为一对多关系使用多个集合恕我直言,只要故事的大小符合 bson 文档限制,就将它们嵌入到主文档中。
这将加快检索过程,还将启用文档级别的 ACID 更新。
一个句子孤立地有意义吗?你能多次重复使用一个句子吗?我认为如果没有故事的背景,一个句子不太可能有意义,而且你也不太可能想要重复使用一个句子。
因此故事文档应该由句子子文档组成。 IE。您将有一个集合(故事),每个故事文档将包含一组句子子文档。这将是您展示的第二个架构。