【发布时间】:2014-04-07 18:11:42
【问题描述】:
当您拥有非规范化架构时,传播更新的最佳方式是什么?是否应该全部在同一个函数中完成?
我有这样的架构:
var Authors = new Schema({
...
name: {type: String, required:true},
period: {type: Schema.Types.ObjectId, ref:'Periods'},
quotes: [{type: Schema.Types.ObjectId, ref: 'Quotes'}]
active: Boolean,
...
})
然后:
var Periods = new Schema({
...
name: {type: String, required:true},
authors: [{type: Schema.Types.ObjectId, ref:'Authors'}],
active: Boolean,
...
})
现在说我想对作者进行非规范化,因为period 字段将始终只使用句点的名称(这是唯一的,不能有两个具有相同名称的句点)。然后说我把我的模式变成了这样:
var Authors = new Schema({
...
name: {type: String, required:true},
period: String, //no longer a ref
active: Boolean,
...
})
现在 Mongoose 不再知道 period 字段已连接到 Period 架构。因此,当一个时期的名称发生变化时,由我来更新该字段。我创建了一个提供如下接口的服务模块:
exports.updatePeriod = function(id, changes) {...}
在此函数中,我通过更改来更新需要更新的期间文档。所以这是我的问题。那么,我应该更新此方法中的所有作者吗?因为那时该方法必须了解 Author 模式和任何其他使用句点的模式,从而在这些实体之间创建大量耦合。有没有更好的办法?
也许我可以发出一个周期已更新的事件,并且所有具有非规范化周期引用的模式都可以观察到它,这是一个更好的解决方案吗?我不太确定如何解决这个问题。
【问题讨论】:
标签: node.js mongodb mongoose denormalization