开发者

Product catalogue storage in mongoDB from an RDBMS perspective

开发者 https://www.devze.com 2023-03-31 07:44 出处:网络
I have a product page with an URL of the form http://host/products/{id}/{seo-friendly-url}, where the \"seo-friendly-url\" part may be something like category/subcategory/product.

I have a product page with an URL of the form http://host/products/{id}/{seo-friendly-url}, where the "seo-friendly-url" part may be something like category/subcategory/product.

The products controller gets the produ开发者_如何学Goct with the specified ID and then ensures that the URL that follows is correct for the product - if it isn't the user is redirected to the appropriate URL (all URLs in the shop are generated correctly though, the redirect is just to maintain a canonical URL in the case of mistyping by the user or the URL changing since Google crawled it etc). The ID ensures fast product look-up, and the part on the end ensures keywords make it into the URL.

To check the URL, I have a SQL view which utilises a recursive common table expression to concatenate the product URL chunk with the URLs of its parent category URLs all the way up the hierarchy (generally just 3 deep).

I've recently came across document oriented storage and I can see it being very useful in a variety of situations (for example, my product entities have tags and multibuy prices and attributes etc all in different tables currently).

So on to my question - how can I achieve the above functionality in mongoDB, or is there a better way to think about it? The naive way would be to retrieve each category in the hierarchy individually, but I'm assuming that would be slow.

Related: I've read in the docs that skip/limit for is slow for large result sets - would this be noticeable for the maximum of say 10 pages of 25 products each likely to be present in a retail website category?


I think your best option is to just store the full slug with the product. Then when you get the product just check to see if the slug matches and if not, redirect. Now, the trade-off is that if you want to rename a category you will need to do a batch job to find all products in the category and change their slugs. The good news is that category renames will be much less common than views (hopefully) so your total load will be reduced.

Not sure how skip and limit are related to this question, except that they both involve mongodb. Anyway for 25 results it's really no problem. Limit isn't slow and in fact can speed things up if less than 100 (default first batch size). Skip can hurt performance, but only by making it as slow as if you fetched all skipped documents w/o the extra network traffic. Therefore I wouldn't skip 1 million docs, but skipping 100 would be fine.


You can model a collection called products, with the document like:

product:{id:someId,category:someCategory,subcategory:someSubCategory,productSlug:somenameslug}

The query to get the product given the id, category and subcategory would be something like:

db.products.find({id:123,category:cat,subCategory:subcat})

This sounds pretty simpleton but given my understanding of your question IMO this should be a good start.

For your other question, there are skip and limit modifiers to help with pagination.

0

精彩评论

暂无评论...
验证码 换一张
取 消