MongoDB model design for Orders with multiple items

Viewed 292

I am currently building an e-commerce site. I am using Express, with Mongodb. I currently have three models.

  1. Users { a user can be an owner or a buyer, I've set up a couple of flags like isOwner, etc}
  2. Products { name, description, and an object of user ref: 'Users' }
  3. Orders { orderItems[which is an array of Products, has an object ID of Products (ref: 'Products')], paymentMethod, and an object of a user ref: 'Users' }

My question is: If a customer comes and add multiple Products to cart the products belong to different Users('Owners') which I am saving in as an array in as "orderItems". How will I make a GET request for the Owner of that specific Item that he just got an order? And is this the correct way of doing it? or should I make another document for OrderItems?

1 Answers

I also working a similar project in these days. I also had a question at this point. In this case, I used separate carts for individual users. Further parallelly, I maintain an array in the state with the products which were added to the card and make POST requests to add cart details to the database. So It is good to make another document for cart items.

Related