3
\$\begingroup\$

Here is my current schema:

Brand:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var BrandSchema = new mongoose.Schema({
  name: { type: String, lowercase: true , unique: true, required: true },
  photo: { type: String , trim: true},
  email: { type: String , lowercase: true},
  year: { type: Number},
  timestamp: { type : Date, default: Date.now },
  description: { type: String},
  location: { },
  social: {
    website: {type: String},
    facebook: {type: String },
    twitter: {type: String },
    instagram: {type: String }
  }
});

Style:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var StyleSchema = new mongoose.Schema({
  name: { type: String, lowercase: true , required: true},
});

Product:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ProductSchema = new mongoose.Schema({
  name: { type: String, lowercase: true , required: true},
  brandId : {type: mongoose.Schema.ObjectId, ref: 'Brand'},
  styleId: {type: mongoose.Schema.ObjectId, ref: 'Style'},
  year: { type: Number }, 
  avgRating: {type: Number}
});

Post:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var PostSchema = new mongoose.Schema({
  rating: { type: number},
  brandId : {type: mongoose.Schema.ObjectId, ref: 'Brand'},
  comment: {type: String},
  productId: {type: mongoose.Schema.ObjectId, ref: 'Style'},
  styleId: {type: mongoose.Schema.ObjectId, ref: 'Style'},
  photo: {type: String}
});

I'm currently making use of the mongoose populate feature:

exports.productsByBrand = function(req, res){
  Product.find({product: req.params.id}).populate('style').exec(function(err, products){
    res.send({products:products});
  });
};

This works, however, I've started reading about performance issues with the mongoose populate, since it's really just adding an additional query.

For my post, it seems that could be taxing. The intent for the post is to be a live twitter / Instagram-like feed. It seems that could be a lot of queries, which could greatly slow my app down.

Also, I want to be able to search products / post / brand by fields at some point.

Should I consider nesting / embedding this data (products nested / embedded in brands)?

What's the most efficient schema design or would my setup be alright -- given what I've specified I want to use it for?


User story:

There will be an admin user.

The admin will be able to add the Brand with the specific fields in the Brand schema.

Brands will have associated Products, each Product*will have a Style / category.

Search:

Users will be able to search Brands by name and location (i'm looking into doing this with angular filtering / tags).

Users will be able to search Products by fields (name, style, etc).


Post:

Users will be able to Post into a feed. When making a Post, they will choose a Brand and a Product to associate the Post with. The Post will display the Brand name, Product name, and Style -- along with newly entered Post fields (photo, comment, and rating).

Other users can click on the Brand name to link to the Brand show page. They can click on the Product name to link to a Product show page.

Product show page:

Will show Product fields from the above Schema -- including associated Style name from Style schema. It will also display Post pertaining to the specific Product.

Brand show page:

Will simply show Brand fields.


My main worry is the Post, which will have to populate / query for the Brand, Product, and Style within a feed.

Again, I'm contemplating if I should embed the Products within the Brand. Would I then be able to associate the index of the Product with the Post for later queries? Or, possibly $lookup or other aggregate features.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.