1

I know there have been a lot of discussion about ref and embed schema in mongoose schema. I am kinda learning node and I have a background of RDBMs . I am working with mongoose. I have a scenario which is quite simple and i have implement it pretty much. The point where I am stuck is either to use embed or ref.

Scenario

My scenario is , I have two models product & categories. The relation b/w them is many to many. First let me show you my models.

Category schema

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);

Product schema

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);

As I am making the front-end too in angularjs I am confused also. I have three documents in my category table which i filled using postman. I am looking at things from front-end angle. I shall have a dropdown of categories and user shall select or multi select the categories for its product.

Now how would I store the categories in Product schema. They are storing right now, But i dont thing I am doing it right. I want to use referencing but may be I am mixing both embed and ref .

I want to get all the product across a single category in my search API and I want to get all the categories of my products too.

I am reading mongoose docs too, but i am getting confused more. Because the example they gave is not relevant to what i describe above.

    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!
  });
});

If i am not wrong they care creating the person and the story at the same time. While I have already made categories. kindly shed some light over this problem.

Usman Iqbal
  • 2,379
  • 5
  • 26
  • 50
  • It really does not matter what "we think". The only thing that really matters is "how you use it". That's the thing that determines whether you embed or reference. See [Mongoose populate vs object nesting](https://stackoverflow.com/questions/24096546/mongoose-populate-vs-object-nesting/24096822#24096822) for a general overview. It's all about if you "read both" or "write both" a lot. If you don't then it's generally best left in separate collections. – Neil Lunn Oct 24 '17 at 07:21
  • could you please help me to understand what if i want to create category table and suppose there are no products what goes in the array of products then ? like this is my schema ..... const CategorySchema = mongoose.Schema({ category_name: { type: String, required: true }, product : [{ type: Schema.Types.ObjectId, ref: 'product' }] }) – Usman Iqbal Oct 24 '17 at 07:25
  • 3
    I pointed you to the post for a reason. It's not the "definitive guide to what do I do", but it does describe what you "should" be looking at. Choosing embedded or referenced ( or even MongoDB itself for that matter ) is **not** A. Because it's cool B. Because my manager said so or any other like reason. It's simply because it what "works" for what you are trying to do. Read it. Read other material like it. If you understand why you are using the engine, then you should understand when to choose which option. No-one here can tell you that. Which is exactly the point of the answer there. – Neil Lunn Oct 24 '17 at 07:35
  • thanks man. let me read further – Usman Iqbal Oct 24 '17 at 07:36

0 Answers0