gpt4 book ai didi

mongodb - 在MongoDB中,Schemas是分开好还是放在一起好?

转载 作者:可可西里 更新时间:2023-11-01 09:13:28 26 4
gpt4 key购买 nike

以故事为例:一个故事由许多句子组成,在我的案例中,故事永远不会超过 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 中。

提前感谢您的任何建议。

最佳答案

一个句子孤立地有意义吗?你能多次重复使用一个句子吗?我认为如果没有故事的上下文,一个句子不太可能有意义,而且您也不太可能希望重复使用一个句子。

因此故事文档应该由句子子文档组成。 IE。您将有一个集合(故事),每个故事文档将包含一组句子子文档。这将是您展示的第二个架构。

关于mongodb - 在MongoDB中,Schemas是分开好还是放在一起好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38483136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com