在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;
每台计算机将具有许多Production_Log文档(超过一百万)。使用嵌入式文档,在测试过程中,我很快达到了每个文档16mb的限制,并且无法在Machine文档中添加任何Production_Log文档。
我的怀疑
这是一种情况,应该使用子文档作为ObjectId引用而不是嵌入式文档吗?
我还能评估其他解决方案吗?
我将访问Production_Log文档以使用聚合框架为每台计算机生成统计信息。我应该在架构设计上考虑什么吗?
预先非常感谢您的建议!
最佳答案
Database normalization不适用于MongoDB
如果将完整信息存储在单个文档中,则MongoDB可以更好地扩展(数据冗余)。数据库规范化要求将不同集合中的拆分数据分开,但是一旦数据增长,就会引起瓶颈问题。
仅使用LOG
模式:
const model = (mongoose) => {
const LogSchema = new mongoose.Schema({
model: { type: String, required: true },
description: { type: String, required: true },
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;
};
读/写操作以此方式可以很好地缩放。
使用Aggregation,您可以处理数据并计算所需的结果。
关于javascript - MongoDB嵌入式文档:大小限制和聚合性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59843357/