Embedded Objects verses Embedded Fields

I am trying to figure out how to effectively link my store database with the products it sells.

Currently I have each as a collection under the Store database. The schema looks like this

Database
Store
Collections
store
product
store_invoice

I am concentrating on stores that sell less than 100 items initially. Because of this low number of items that each store is going to sell I am inclined to embed the products in the store collection. If I do this, I can eliminate the product collection because the products that the store sells will be embedded.

If I embed the product as a subdocument, do I embed it as a document or as an object? say like this

“product”: Object
“name”: “Product Name”
“short description” : “description”
“category tag”: [ “tag”, “tag”]
“price” : price

My confusion is that the relationship is a one to many. The product will have a one relationship and the store will have a many relationship.

The current way I have it set up is that I add the product and after I add it to the product collection, I add the _id of the product to the store record in a product array.

Hi @David_Thompson ,

If you embed only the ids of the products in a store document as an array its not considered embedding but having a reference in the store documents.

It describes a 1 to many relationship between a store and products. However, it is possible to have a many to many relationship where a product is placed in many stores and many stores have this product , which can also work in that case.

I would still suggest to consider embedded products which sit as a whole sub-doument array in the store collection.

StoreName : ... ,
Products : [ {productId : ... , ProductName : ..., Price : ... } , ... ]
}

However, its also possible to consider a hybrid solution when you have a product in both collections and you embed only the “rarely changed”. Fields for better list presenting keeping vital fields in both collections.

Best regards
Pavel

If I do this, in mongo atlas, is the embedded document an array of objects?

For the record, would it look like this:

StoreName: ABC Inc
Product: Object
[
Name: “Product Name”
Ldescription: “Long Desctription”
]
The product Id would be the _id field that is auto generated with the object.

Am I on the right track?

Hi @David_Thompson ,

MongoDB Atlas runs MongoDB versions similar to the ones you can install locally.

Having the productId in store document the same as in reference product collection (_id) sounds right.

Thanks
Pavel

Ok… Since I am new to this and primarily familiar with primary keys and foreign keys, I have one more question for you.

My original thinking was to put the store _id in the product record (to solve multiple stores selling the same product) and the product _id in the store (so the store can access which products are sold in it)…

On rethinking this, I only think it is necessary to put the store name in the product record. (Each store will have product records unique to their store)

_id : 6107757055d471a3e5658c13
Sku : 10001010
Name : "Test Product 1"
Ldesc : "Sample Long Description"
Sdesc : "Sample Short Description"
Price : 19.99
Length : 13
Width : 23
Height : 33
Weight : 1.45
Category : [ ]
Image : [ ]
Tag : [ ]
**StoreName : "ABC Inc"**
Uid : "56E26553-FD6F-4D52-918E-5E197A60EEC5"

The product collection is located in the Store Database and when the store is loaded into the view, you can do an index search for all products that StoreName matches the store… Is this in the right direction?

The only question is whether I only index on the StoreName, to use all the fields in the document, I would still have to examine all the documents that match the index query right. Is it common practice to create an index with almost all the fields to minimize collection scans? Or do I wait to structure this until I am having performance degradation.

Thanks

Dave

Hi @David_Thompson ,

There are multiple variations that this embedding can happen and it depands in the main query patterns.

In my opinion if the store information is loaded first it sounds like product lists are the one to folllow. If products will be embedded in the store document than just by querying the store single document you will get the product list or at least a subset of it to show in the ui.

Now think on a case of clicking one of the products in the list than you can query the full document from the products collection by the specific id.

There is no need to index all fields and it is not advisable. You need to index only the fields that use as query predicts or require unique constraints.

Read here :

For example indexing a products.productId or products.productName in store collection might help finding all stores with specific products.

Since each store will duplicate a product it can store a per store attributes for that product as price or discount.

For those reasons having product arrays in store documents while perhaps keeping a product catalog seperately makes most sense in my opinion.

Thanks
Pavel

1 Like