哪种猫鼬模型会更有效率?
Which mongoose model would be more efficient?
我是 no-sql
的新手。我正在尝试在 nodejs 中构建一个简单的电子商务应用程序。现在对于产品,我需要构建 CRUD 操作,以便只有所有者可以编辑它们,其余的有 READ-ONLY
访问权限。主要问题是哪种实施方式更好?
我目前的代码是这样的
var mongoose = require('mongoose');
module.exports = mongoose.model('product',new mongoose.Schema({
owner : {type: String},
title : {type: String},
...
}));
所有者实际上是我的用户模型中的 _id
。基本上这就像外键。这是绕过的有效方法还是我应该在 user model
中添加一个数组来存储他拥有的对象列表?
此外,我想验证我刚刚为所有者所做的事情,将 UID
存储在 String
中是否是最佳实践,或者我应该做其他事情来引用用户模型。
在此先感谢您的帮助。
文档数据库的全部要点是你不应该有外国关系;您的文档需要的所有数据都应该在文档中进行非规范化。
因此,在您的 product
文档中,您应该复制所需的所有所有者详细信息。您 可以 存储它们的 _id
以及查找,但不要为此使用字符串,使用实际的 ObjectId()
.
有关反规范化的更多信息,请参阅 The Little MongoDB Book
Yet another alternative to using joins is to denormalize your data. Historically, denormalization was reserved for performance-sensitive code, or when data should be snapshotted (like in an audit log). However, with the ever- growing popularity of NoSQL, many of which don’t have joins, denormalization as part of normal modeling is becoming increasingly common. This doesn’t mean you should duplicate every piece of information in every document. However, rather than letting fear of duplicate data drive your design decisions, consider modeling your data based on what information belongs to what document.
我是 no-sql
的新手。我正在尝试在 nodejs 中构建一个简单的电子商务应用程序。现在对于产品,我需要构建 CRUD 操作,以便只有所有者可以编辑它们,其余的有 READ-ONLY
访问权限。主要问题是哪种实施方式更好?
我目前的代码是这样的
var mongoose = require('mongoose');
module.exports = mongoose.model('product',new mongoose.Schema({
owner : {type: String},
title : {type: String},
...
}));
所有者实际上是我的用户模型中的 _id
。基本上这就像外键。这是绕过的有效方法还是我应该在 user model
中添加一个数组来存储他拥有的对象列表?
此外,我想验证我刚刚为所有者所做的事情,将 UID
存储在 String
中是否是最佳实践,或者我应该做其他事情来引用用户模型。
在此先感谢您的帮助。
文档数据库的全部要点是你不应该有外国关系;您的文档需要的所有数据都应该在文档中进行非规范化。
因此,在您的 product
文档中,您应该复制所需的所有所有者详细信息。您 可以 存储它们的 _id
以及查找,但不要为此使用字符串,使用实际的 ObjectId()
.
有关反规范化的更多信息,请参阅 The Little MongoDB Book
Yet another alternative to using joins is to denormalize your data. Historically, denormalization was reserved for performance-sensitive code, or when data should be snapshotted (like in an audit log). However, with the ever- growing popularity of NoSQL, many of which don’t have joins, denormalization as part of normal modeling is becoming increasingly common. This doesn’t mean you should duplicate every piece of information in every document. However, rather than letting fear of duplicate data drive your design decisions, consider modeling your data based on what information belongs to what document.