【发布时间】:2014-12-13 11:29:03
【问题描述】:
我有一个看起来像这样的 Mongoose 架构:
ManifestSchema = new Schema({
entries: [{
order_id: String,
line_item: {}, // <-- resolved at run time
address: {},// <-- resolved at run time
added_at: Number,
stop: Number,
}]
}, {collection: 'manifests', strict: true });
在代码的某个地方我有这个:
Q.ninvoke(Manifests.findById(req.params.id), 'exec')
.then(function(manifest)
{
// ... so many things, like resolving the address and the item information
entry.line_item = item;
entry.address = order.delivery.address;
})
我面临的问题是,如果没有在架构中定义地址和 line_item,当我在运行时解决它们时,它们不会返回给用户,因为它们不在架构中......所以我添加了它们...这导致了我另一个不需要的行为:当我将对象保存回来时,地址和 line_item 都与清单对象一起保存,这是我想避免的。
是否可以在运行时向架构添加字段,但在返回时不保存它们?
我试图在猫鼬中使用“虚拟”,但它们确实提供了我需要的东西,因为我不是从模式创建模型,而是从数据库返回。
【问题讨论】:
标签: node.js mongodb mongoose schema