节点的Js – 我需要ref或embedded我的Mongoose架构?

我知道在mongoose schema有很多关于refembed模式的讨论。 我有点学习node ,我有一个RDBMs的背景。 我正在和mongoose一起工作。 我有一个非常简单的场景,我已经实现了它。 我卡住的地方是要么使用embedref

脚本

我的情况是,我有两个模型productcategories 。 它们之间的关系是many to many 。 首先让我告诉你我的模型。

类别架构

 const mongoose = require('mongoose'); //function to call monggose schema const CategorySchema = mongoose.Schema({ category_name: { type: String, required: true } }) const Category = module.exports = mongoose.model('Category', CategorySchema); 

产品架构

 const mongoose = require('mongoose'); const ProductSchema = mongoose.Schema({ product_name: { type: String, required: true }, product_rating: { type: Number, required: true }, product_price: { type: String, required: true }, product_btc: { type: String, required: true }, product_mode: { type: String, required: true }, product_date: { type: String, required: true }, product_hasrate: { type: String, required: true }, product_powerConsumption: { type: String, required: true }, product_efficiency: { type: String, required: true }, product_voltage: { type: String, required: true }, product_frequency: { type: String, required: true }, product_shipment: { type: String, required: true }, product_warranty: { type: String, required: true }, product_contact: { type: String, required: true }, category: [mongoose.Schema.Types.Mixed] }) const Product = module.exports = mongoose.model('Product', ProductSchema); 

由于我在angularjs做了前端,我也很困惑。 我在我的category表中有三个documents ,我用邮差填充。 我从前端angular度看东西。 我将有一个类别的下拉列表,用户应select或多重select其产品类别。

现在,我将如何将Product架构中的类别存储。 他们现在正在储存,但我不这样做是正确的。 我想使用referencing但可能是我混合embedref

我想要在我的searchAPI中获得所有产品的所有类别,我也想获得我的产品的所有类别。

我正在阅读mongoose文档,但我越来越困惑。 因为他们给出的例子与我上面描述的不相关。

  var author = new Person({ _id: new mongoose.Types.ObjectId(), name: 'Ian Fleming', age: 50 }); author.save(function (err) { if (err) return handleError(err); var story1 = new Story({ title: 'Casino Royale', author: author._id // assign the _id from the person }); story1.save(function (err) { if (err) return handleError(err); // thats it! }); }); 

如果我没有错,他们会在同一时间创造人和故事。 虽然我已经做了类别。 请对这个问题提出一些看法。