2

I am creating simple RestApi using MongoDB, Mongoose, Node.js, Express.js and In which there will be multiple users and many more users can sign up and there will be an entry for each user in user collection(database). And my user Schema(users.js) will be like

var mongoSchema = mongoose.Schema;
var userSchema ={
    mobileno: {
        type:String
    },
    firstname: {
        type:String
    },
    lastname: {
        type:String
    },
    facebookid: {
        type:String
    },
    userimage: {
        type:String
    }
}
module.exports = mongoose.model('user', userSchema);

Now each user can save one or more products as follows

enter image description here

Now which one would be the best solution:

  1. Should I use separate collection (products collection with one field like mobile no for reference)
  2. Or Should I use subdocument or nested objects

My personal choice is second one but I am new in MongoDB and Mongoose environment. Please help me what I need to change in users schema.

1

4 Answers 4

1

You can do it like this :

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema{
  mobileno: {
    type:String
  },
  firstname: {
    type:String
  },
  lastname: {
    type:String
  },
  facebookid: {
    type:String
  },
  userimage: {
    type:String
  }
}

var productSchema = new Schema{
 _user:{type: Schema.ObjectId, ref: 'user'}
 prod_name: {
    type:String
 },
 prod_cost: {
    type:String
 }
}
module.exports = mongoose.model('user', userSchema);
module.exports = mongoose.model('products', productSchema);

You can reference user to product table (i,.e, just like joins in mysql)

And populate while fetching the profile :

Models.products.find(criteria, projection, options).populate([
    {path: '_user', select: 'prod_name, prod_cost'},
]).exec(callback);

Thanks & Regards

Sign up to request clarification or add additional context in comments.

1 Comment

Is this correct approch?? Because It seems opposite to what I want.
0

The second choice is generally preferred. The schema for the product key would look like -

products: [productname: String, productdesc: String, ]

However, in some cases the first option to create a separate product collection makes more sense. This would happen if the collection depends on some external source and is fairly dynamic in nature. Here you would find it easier to update at one place rather than a lot of places. In this case, you can have a schema like

products: [productSchema]

Here for getting the product details, you would need to do $lookup operations

Comments

0

I think, the better way is other way around.

  • Save the product as a collection and user as another collection.
  • In product collection, save the reference of user.

Then use populate() to populate user in product, if you want.

3 Comments

Seems correct approach because this is exactly the first idea which came in mind when I got this task.
But this approach becomes lengthy because when I will delete any user so also I need to delete its saved products by running separate function or is there any cascade delete like option.
yes. you have this option. Please have a look stackoverflow.com/questions/30255638/…
0

After reading existing stack overflows answers I think following approach would be right. Please suggest or correct me if I am wrong.

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

var productSchema = new Schema{
 prod_name: {
    type:String
 },
 prod_cost: {
    type:String
 }
}

var userSchema = new Schema{
  mobileno: {
    type:String
  },
  firstname: {
    type:String
  },
  lastname: {
    type:String
  },
  facebookid: {
    type:String
  },
  userimage: {
    type:String
  },
  products: [productSchema]
}


module.exports = mongoose.model('user', userSchema);
module.exports = mongoose.model('products', productSchema);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.