Home  >  Q&A  >  body text

mongoengine - MongoDB怎么设计多级分类好?

文章以及课程模型需要定义所属分类,该分类是多级的,MongoDB中怎么设计比较合理?如果直接定义在文档里,担心一致性的问题!

伊谢尔伦伊谢尔伦2736 days ago859

reply all(6)I'll reply

  • 怪我咯

    怪我咯2017-04-22 09:01:46

    Collections or embedded documents in MongoDB
    Yes, related special thoughts

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-22 09:01:46

    This is what I did:

    {
        'name': '苹果',
        'category1': '生鲜',
        'category2': '水果'
    }
    

    Easy to search and rank

    reply
    0
  • 怪我咯

    怪我咯2017-04-22 09:01:46

    If the embedded document create/delete/update/ is operated frequently, and there are requirements for sorting when fetching data, it is better to have it at most three levels, and do not embed it too deeply.
    I'm out now because of some collection 内嵌得太多,导致很多操作相当不方便,所以又得花很多功夫来把它们 extract.
    Of course, if you only want to store data once and the data operations are mainly reading, you don't need to think so much.
    Like the course article classification you proposed, if I understand the requirements correctly, can it be like this:

    "_id":
    "name" : 
    "category" : [ 
        {
            "_id" : 
            "name" : 
            "degree" :
        }, 
        {
            "_id" : 
            "name" : 
            "degree" :
        }, 
    ]
    

    degree表示课程类别中的级数,这样一个文章在课程内的类别在读的时候按照 degree to read. For example, an article is "Newton's Second Law" and its course classification is (Physics -> Force). Then save it as:

    "_id":
    "name" : 
    "category" : [ 
        {
            "_id" : 
            "name" : '物理'
            "degree" : 1
        }, 
        {
            "_id" : 
            "name" : '力'
            "degree" : 2
        }, 
    ]
    

    Press categorydegreesort when reading data.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-22 09:01:46

    This is actually a design trade-off, and it has nothing to do with mongdb. Even mysql still needs to think about this issue!
    Personally, I feel that we still have to start from the business point of view and think about whether the business data to be analyzed meets the constraints, for example, whether it requires real-time consistency or final consistency.

    On another level, for mongdb itself, its embedded documents are not much different from ordinary documents. However, if the embedded document you want to add is one that changes frequently, especially if there is no limit on the size, it is more appropriate to use association. !

    reply
    0
  • ringa_lee

    ringa_lee2017-04-22 09:01:46

    Category can be defined as a nested document because you won’t update it often.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-22 09:01:46

    Use quotes.

    reply
    0
  • Cancelreply