亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

mongodb 聯(lián)合查詢問題
高洛峰
高洛峰 2017-04-21 11:18:37
0
3
704
class Subject(Document):
'''
新聞主題信息
'''
author_ref = ReferenceField(Account,dbref=False)  #關(guān)聯(lián)用戶
when = DateTimeField(default=datetime.datetime.now, required=True)                                  
title = StringField(max_length=50)  `                    # 文章標(biāo)題

`

    class Comment(Document):
'''
評(píng)論
'''
subject_ref = ReferenceField(Subject,dbref=False)   #關(guān)聯(lián)主題
display_name = StringField(max_length=50)
author_ref = ReferenceField(Account,dbref=False)
content = StringField()                          # 內(nèi)容

問題:第一個(gè)是主貼子表,第二個(gè)是評(píng)論表,現(xiàn)在要查詢出主貼的列表,并且統(tǒng)計(jì)出主貼的評(píng)論數(shù) 有什么好的辦法 ?

高洛峰
高洛峰

擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...

reply all(3)
小葫蘆

Just talking about my needs. The number of comments should have an additional field attribute on the main post. Why do you have to count every time? If a post is very popular and has millions of comments, do you still have to count?

迷茫

Chat with the big guys. There are several ways. This is a very interesting question.

  1. Keep current schema
    MongoDB's document model determines that it has no join, so it has to be queried twice. Take a look at the syntax of the Object-Document-Mapper you are using.

  2. Embed comments in the post and count them on the client side.
    During Query, the entire document is brought to the client and counted. This part of the data transfer is wasted. But when a single post is displayed, all the information is available. If you don’t have many comments, this schema is the recommended approach. But we can improve further.

  3. Embed comments in posts and count them with aggregation framework.
    Use db.posts.aggregate(...), unwind, group. This method is a bit overkilling...

  4. Embed comments in the post and add a counting cache.
    You can also keep it as it is as suggested above, but if they are in a doc, an update will count the comments $push到comments里,同時(shí)$inc, which maintains consistency very well. When querying, just write out the second projection parameter without comments. For example, you want to find the nearest 10.

db.posts.find({}, { comments: -1 }).sort({"when": -1}).limit(10)
伊謝爾倫

I searched a lot of information, and finally added a separate statistical attribute

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