0

I have an online shop, kind of like eBay. There are sellers and the sellers products. Each seller can have 0 or more products that he is willing to sell. Normally, there are around 30 per person.

Right now I have one document per person, and that document contains the users info and a sub-document that contains every product he is selling (including all the photos that the product has, prices, tags, etc).

Am I abusing MongoDB's documents? Should I be doing it in another way?

alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • @costa Buyers can see all products (so they can buy them). I'm just not that used to embedding everything in a single document, and I'm worried for the implications it could lead. What if a seller decides he wants to sell 5000 items? Could MongoDB handle that? – alexandernst Jan 29 '15 at 18:25
  • 2
    http://docs.mongodb.org/manual/reference/limits/ – boggy Jan 29 '15 at 18:32
  • I'm calling this a duplicate. The embed vs. reference question has been asked in many different ways. – jcollum Jan 29 '15 at 19:04

2 Answers2

1

I would suggest that you move your products into a separate collection (e.g. products). Embedded documents should only be used when the document you're embedding is small, or the amount is limited. Additionally, having products in a separate collection will allow for greater flexibility when selecting documents.

In your case, you would likely be sometimes selecting a single product (e.g. a view for viewing a single product) so it would make sense to keep products in a separate collection.

Jon
  • 2,932
  • 2
  • 23
  • 30
  • That helped me a lot. So I should be actually storing all the products in another collection as I'll need to show random products, query them in a specific order, or with specific tags (show me all red products, or, show me all products heavier than 2kg). Thank you! Let's see if others can add something. – alexandernst Jan 29 '15 at 18:53
1

I would suggest moving products to a separate collection.

MongoDB data schemas are best when based on the usage requirements. In this case, a user browsing products is going to very rarely be interested in all the products that a different user is selling. Usually, they're going to be interested in products that match search criteria about price and item description or item title.

So imagine a search for the most recently posted items given your proposed data schema. Your query will have to search through every user's array of product subdocuments and check the timePosted. Whereas a product collection could easily have an index on timePosted and not require touching unnecessary (user and each user's other products) documents.

The use case for seeing a single product page is also important to consider and also indicates using a separate product collection.

Even when you do need to show products that a particular user posted, it will not be difficult in a product collection to search by user_id.

NoOutlet
  • 1,949
  • 1
  • 14
  • 22