1

I have a question regarding mongoDB, namely mongoose:

Let's say I have 2 models: 1) Product model 2) User model

const ProductSchema = new Schema({
  title: String,
  description: String,
  product_type: String,
  image_tag: String,
  created_at: Number,
  price: Number
});
const Product = module.exports = mongoose.model('product', ProductSchema);

const UserSchema = new Schema({
  login: String,
  email: String,
  password: String,
  purchases: [{
    type: Schema.Types.ObjectId,
    ref: 'product'
  }]
});
const User = module.exports = mongoose.model('user', UserSchema);

When the user buys some goods, I just add product to purchases array in user model(using push method). If I need to match purchase's ID to its full description I use populate.

The problem is I need somehow to control quantity of each purchase made by user -> I need additional field in each purchase object inside array...something like quantity or total, like that:

const UserSchema = new Schema({
     ...
      purchases: [{
        type: Schema.Types.ObjectId,
        ref: 'product',
        quantity: {
          type: Number,
          default: 1
        }
      }]
    });

I'm stuck and have no clue how to implement this. Example above does not work.

rickjerrity
  • 804
  • 1
  • 9
  • 15
Apheliont
  • 45
  • 1
  • 4
  • 1
    Take `quantity` field as a root field in Userschema and increase its quantity on every purchase using `$inc` – Ashh Jul 27 '18 at 18:08
  • The thing is that I need to control quantity of every purches, that is to say if user buy some good and it is not present in purches array yet, it should be added as an object with quantity field. If user buy the same good more than once quantity counter should be increased – Apheliont Jul 27 '18 at 18:59
  • What is the error ? – radhey shyam Jul 27 '18 at 19:05
  • No errors but no added field also. I see only Id of purches after pushing it into array. And even if I populate data no field...everything is of no use cry – Apheliont Jul 27 '18 at 19:08
  • May be you will go with the radheyshyam but in future it will put you in trouble when you do some populate or aggregation with that type of nested data – Ashh Jul 27 '18 at 22:06

1 Answers1

1

Try this, this way will definitely work:

const UserSchema = new Schema({
    purchases: [{
            ref: {
                 type: Schema.Types.ObjectId,
                 ref: 'product'
            },
            quantity: {
              type: Number,
              default: 1
            }
          }]
 });
radhey shyam
  • 759
  • 6
  • 9
  • Populagte `ref` key or change the key and populate that one to get the product document information. – radhey shyam Jul 27 '18 at 19:11
  • Thank you radhey shyam. It definitely works out! Now i need to find a way to populate ref ;) – Apheliont Jul 27 '18 at 19:42
  • My pleasure you just need to pass key ref inside the populate like: `user.findOne(Criteria).populate({ path: "" }).then(result => { console.log(result) }).catch(error => { console.log(error); })` – radhey shyam Jul 27 '18 at 19:44