javascript - Mongoose:在属性中引用模式或添加为数组?哪个更好
问题描述
例如,如果我有两个模式User和Post,我应该在 Post 的属性中添加 User 引用,还是在 User Schema 中添加 Post 模式作为数组?这是更好的性能明智(和其他方面)。
var PostSchema = new mongoose.Schema({
title: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
});
或者
var UserSchema = new mongoose.Schema({
name: String,
posts: [PostSchema]
});
解决方案
var PostSchema = new mongoose.Schema({
title: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
});
我认为这种方式比其他方式更好。
推荐阅读
- excel - 试图让 vb 代码接受部分字符串匹配
- python - Python - 试图将hrefs放入可用列表中
- android - Android 导航组件总是返回到起始目的地
- azure - 如何使用 Azure 数据工厂将 Azure 分析服务中的数据存储到 Azure Datalake?
- tensorflow - 如何在张量流中创建对组合
- git - 将 git bash 设置为 VSCODE 中的默认终端
- sql-server - 如何替换本机编译模块中的函数 REPLACE()?
- python - 文本到元组列表
- c++ - fstream不是逐个字符而是整数
- python - 基于其他 2 个数据框条件的新数据框