【发布时间】:2014-08-27 19:57:04
【问题描述】:
这是一个带有 REST API 的应用程序的数据模型,它有望扩展到相当大(如果我幸运的话)。我读过difficulties that can arise with Arrays in MongoDB,所以我很犹豫要不要使用它们。以下是应用程序的具体要求:
- 数组将包含对象
- 数组对象会经常修改
- 会有很多对数组中的对象执行的查询
- 数组不会超过最多 30 个对象
- 我希望 REST API 发送带有数组属性的数据,因为它们更易于使用。当然,我们将其保存在数据库中的方式可能会有所不同...
鉴于这些特定要求,以下哪些模型应该使用 MongoDB 更好地扩展?
Order.products 的数组
var OrderSchema = new Schema({
customer_name: String,
products: {
type: Array,
default: []
}
});
嵌套对象 Order.products
// Converting the array items to nested objects before saving, and performing the reverse before sending them back to the User.
// For example, Order.products = { one: {}, two: {}, three: {} }
var OrderSchema = new Schema({
customer_name: String,
products: {
type: Schema.Types.Mixed,
default: {}
}
});
或者将这些项目放在单独的数据模型中......
var OrderProductSchema = new Schema({
order_id: { type: Schema.ObjectId },
product_title: String,
product_price: Number
});
【问题讨论】:
-
产品是全球性的还是仅存在于特定订单中?那么,每个订单分配给多个订单的产品数量是有限的,还是订单之间不共享的产品数量是无限的?我现在陷入了网上商店的隐喻之中。
-
产品数量不限,订单之间不共享。
-
相应地编辑了答案