Mongo DB 带有标签和子项的查询 WHERE IN 的最佳实践

发布于 2024-12-04 16:22:10 字数 778 浏览 7 评论 0原文

好吧,我正在计划一个集合模式,并且我对如何将数据嵌入标签字段有疑问。

我有一个名为产品的集合:

products
       ->_id
       ->product_name
       ->tags

然后我有一个标签集合:

tags
    ->_id
    ->tag_name

并且我有一个tags_child集合:

tags_childs
      ->_id
      -> id_tag
      ->tag_child_name

现在我需要将标签和tags_childs保存到产品集合中,所以我认为在产品集合中保存一个字段是很好的:

标签:[ [_id_tag]:[_id_tag_child,_id_tag_child,...等],[_id_tag]:[_id_tag_child,_id_tag_child,...等]]

但我认为这是不对的方式,因为我需要能够查询产品集合,按标签和子标签进行过滤。

例如,我需要通过以下方式过滤产品: +产品名称:'烤牛肉' + 标签:'热门' + 子标签:“酱”

或过滤条件: +产品名称:'烤牛肉' + 标签:“热门” + 子标签:'酱' + 子标签:'刀' + tag:'dinner'

过滤时始终需要父标签,而子标签是可选的。

最后如何实现正确的集合来执行此类查询?

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 and 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-11 16:22:10

阅读本文;我在这里详细描述了您的架构:

确实没有理由保留这个关键表,因为最终它们都只是标签。您仍然可以使用上述建议的架构在数据库中搜索多个标签;并显示上述架构中的所有可用标签及其关系;这样,它会清理整个数据库,而不是用 MongoDB 不需要的额外表来弄乱它。

我强烈建议您重新阅读这本电子书,以加强对 Mongo 赋予您的 NoSQL 格式的理解。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文