dousi9215 2011-09-12 15:50
浏览 47
已采纳

Mongodb父母和孩子与其他集合领域相关的标签

Well , i'm going to set up my mongo db schema for a little site, what i need is to reproduce this schema into mongo db collections:

product
        -> tag1
              ->child_tag1
              ->child_tag2
        -> tag2
              ->child_tag1
              ->child_tag2
        -> tag3
              ->child_tag1
              ->child_tag2

which is the best way to reproduce this schema with mongo ?

For sure i need to be able to change a tag or child tag without update all collection objects :) [EDITED]

Well cause of i know my question is not so clear, i need to clarify that i'm trying converting a mysql db site onto a mongoDb site.

So i have 4 tables in mysql:

products

id(AUTO) | product_name | qty
 1          biscuits      34
 2          limonade      29

tags

id(AUTO) |  tag_name 
  1            sugar
  2            eggs
  3            vitamine C

tags_childs

id(AUTO) | id_tag | tag_child_name
  1          1        glucouse
  2          2        protein
  3          2        chicken
products_tags

id_product | id_tag | id_tag_child 
  1            1         NULL
  1            1         1
  1            2         3
  2            3        NULL

so i use products_tags table to join over 4 tables (and this is why heating joins i'm switching to mongoDb :))

so i can reproduce this scenario with mongodb collections and objects?

thx :)

  • 写回答

1条回答 默认 最新

  • douxing2652 2011-09-12 20:40
    关注

    Just wanted to make sure I'm understanding what your above document shows; essentially Many Products can have Many Tags, and Tags can have children under them (Coding tag -> Php is child tag of Coding).

    With that being my understanding of the above question here's how I would structure your Mongo Collections:

    Products Collection:

    {
       "ProductName":"Sweet Jelly",
       "Qty":9,
       "Tags":[
         ListOfTagIds
        ]
     }
    

    The list of TagIds would be the Unique Identifiers which mongoDB automatically assigns to each of the new fields created.. see here

    From there I'd create a Tags Collection:

    {
      "TagName":"Coding Languages";
      "ChildTags":[
          $_idOfChildren
       ]
    }
    

    Then if your tag had a child:

    { 
      "TagName":"PHP",
    }
    

    For this Parent / Child relationship you can read a bit more about it from this excellent mongo resource located here: Mongo Book - Page 16 starting with No Joins.

    Good Luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元