【发布时间】:2020-01-22 04:03:17
【问题描述】:
在 MongoDB 的文档中,建议将尽可能多的数据放在一个文档中。还建议不要使用基于 ObjectId ref 的子文档,除非这些子文档的数据必须从多个文档中引用。
在我的例子中,我有这样的一对多关系:
日志架构:
const model = (mongoose) => {
const LogSchema = new mongoose.Schema({
result: { type: String, required: true },
operation: { type: Date, required: true },
x: { type: Number, required: true },
y: { type: Number, required: true },
z: { type: Number, required: true }
});
const model = mongoose.model("Log", LogSchema);
return model;
};
机器架构:
const model = (mongoose) => {
const MachineSchema = new mongoose.Schema({
model: { type: String, required: true },
description: { type: String, required: true },
logs: [ mongoose.model("Log").schema ]
});
const model = mongoose.model("Machine", MachineSchema);
return model;
};
module.exports = model;
每台 Machine 都会有许多 Production_Log 文档(超过一百万)。使用嵌入式文档,我在测试期间很快达到了每个文档 16mb 的限制,我无法再向 Machine 文档添加任何 Production_Log 文档。
我的疑惑
在这种情况下,是否应该使用子文档作为 ObjectId 引用而不是嵌入文档?
还有其他我可以评估的解决方案吗?
我将访问 Production_Log 文档以使用聚合框架为每个 Machine 生成统计信息。我应该对架构设计有任何额外的考虑吗?
非常感谢您的建议!
【问题讨论】:
标签: javascript node.js mongodb mongoose mongoose-schema