开发者

Mongo DB best practice for a query WHERE IN with tags and childs

开发者 https://www.devze.com 2023-04-05 13:14 出处:网络
Well i\'m planning a collection schema and i have dubt to how embed data inside tag field. i have a colletion named products:

Well i'm planning a collection schema and i have dubt to how embed data inside tag field.

i have a colletion named products:

products
       ->_id
       ->product_name
       ->tags

i have then a tags collection:

tags
    ->_id
    ->tag_name

and i have a tags_child collection:

tags_childs
      ->_id
      -> id_tag
      ->tag_child_name

now i need to save tags and tags_childs into products collection, so i tought it was good to save in products collection a field:

tags: [[_id_tag]:[_id_tag_child,_id_tag_child, ... etc],[_id_tag]:[_id_tag_child,_id_tag_child, ... etc]]

but i think is not the right way, cause i need to be able to query on products collection, filtering by tags a开发者_如何学运维nd child_tags.

So for example i need to filter products by: +product_name: 'roastbeef' + tag:'hot' + child tag : 'sauce'

or filter by: +product_name: 'roastbeef' + tag: 'hot' + child tag:'sauce' + child tag:'knife' + tag:'dinner'

Parent tags are always required when filtering, while child tags are optional.

How do you implement a right collection to do this type of query at the end?


Reading over this; I described your schema a bit in detail here:

  • Schema Advice

There really isn't a reason to be holding this pivotal table, as in the end they are all just tags. You can still search for multiple tags within your database by going with the above suggested schema; along with showing all your available tags to and their relationship from the above schema; this way it cleans up your entire DB rather than muddying it with an extra table which MongoDB just doesn't need.

I'd highly recommend re-going through this e-book to just strengthen the understanding around the NoSQL format Mongo empowers you with.

  • Mongo Free eBook PDF
0

精彩评论

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