mongoengine - MongoDB怎么设计多级分类好?
伊谢尔伦
伊谢尔伦 2017-04-22 08:59:46
0
6
900

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(6)
刘奇

Collections or embedded documents in MongoDB
Yes, related special thoughts

Peter_Zhu

This is what I did:

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

Easy to search and rank

刘奇

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.

伊谢尔伦

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. !

左手右手慢动作

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

Ty80

Use quotes.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template