Python 如何从另一个集合中获取包含键的文档计数

Python 如何从另一个集合中获取包含键的文档计数,python,mongodb,flask,Python,Mongodb,Flask,我正在建立一个个人博客 我有两个收藏;帖子和评论 帖子看起来像这样 { '_id': ObjectId('5d7bf67d30bb02db73fd491d'), 'title': 'Comments', 'post_author': ObjectId('5d7008ce6d6a577820420467'), 'content': "There is a comment", 'date_posted': datetime.datetime(2019, 9,

我正在建立一个个人博客

我有两个收藏;帖子和评论

帖子看起来像这样

{
    '_id': ObjectId('5d7bf67d30bb02db73fd491d'),
    'title': 'Comments',
    'post_author': ObjectId('5d7008ce6d6a577820420467'),
    'content': "There is a comment",
    'date_posted': datetime.datetime(2019, 9, 13, 20, 5, 17, 367000)
}
评论是这样的

}    '_id': ObjectId('5d7c9717c9fc4f7b8e19e906'), 
     'user': ObjectId('5d7008ce6d6a577820420467'), 
     'post_id': ObjectId('5d7b5aee0de3faa4cc8a1dc0'), 
     'title': 'is this going to get super nested?',
     'content': 'this is getting weird',
     'date_posted': datetime.datetime(2019, 9, 14, 7, 30, 31, 587000)
}

{% for post in posts %}
        <div class="posts z-depth-2">
            <div class="post-card">
                <span class="post-title"><a href="{{ url_for('post', post_id=post._id) }}">{{ post.title }}</a></span>
                {{ comment_count }}
                {% if post.date_posted %}
                <span class="post-date">{{ post.date_posted.strftime("%d %b %Y") }}</span>
                {% endif %}
                <hr>
            </div>
            <p>{{ post.content }}</p>
        </div>
{% endfor %}
我正在努力为每一篇文章收集评论

我一直在尝试循环浏览所有帖子,并使用count获得每个帖子的评论数,如下图所示

def comment_count():
    posts = mongo.db.posts.find().sort("_id", -1)
    for post in posts:
        post_id = post['_id']
        count = mongo.db.comment.count({'post_id': post_id})
        return count
我的模板看起来像这样

}    '_id': ObjectId('5d7c9717c9fc4f7b8e19e906'), 
     'user': ObjectId('5d7008ce6d6a577820420467'), 
     'post_id': ObjectId('5d7b5aee0de3faa4cc8a1dc0'), 
     'title': 'is this going to get super nested?',
     'content': 'this is getting weird',
     'date_posted': datetime.datetime(2019, 9, 14, 7, 30, 31, 587000)
}

{% for post in posts %}
        <div class="posts z-depth-2">
            <div class="post-card">
                <span class="post-title"><a href="{{ url_for('post', post_id=post._id) }}">{{ post.title }}</a></span>
                {{ comment_count }}
                {% if post.date_posted %}
                <span class="post-date">{{ post.date_posted.strftime("%d %b %Y") }}</span>
                {% endif %}
                <hr>
            </div>
            <p>{{ post.content }}</p>
        </div>
{% endfor %}
{%for posts in posts%}
{{comment_count}}
{%if post.date_posted%}
{{post.date_post.strftime(“%d%b%Y”)}
{%endif%}

{{post.content}}

{%endfor%}

当我从模板中调用comment_count来显示每个帖子的评论数时,无论实际评论数是多少,它都会为所有帖子返回6。我希望它能给出一个正确的计数。

你可以用它来计算。您需要运行的操作由
post
集合上的管道组成,该管道对
comments
集合执行操作,并创建一个额外字段,返回管道步骤生成的数组中的项目计数

考虑运行以下聚合操作:

def posts_with_comment_count():
    pipeline = [
        { "$lookup": {
            "from": "comment",
            "localField": "_id",
            "foreignField": "post_id",
            "as": "comment_count"
        } },
        { "$addFields": {
            "comment_count": { "$size": "$comment_count" }
        } },
        { "$sort": { "_id": -1 } }
    ]
    posts = list(mongo.db.posts.aggregate(pipeline))

    return posts

你可以用它来做这个。您需要运行的操作由
post
集合上的管道组成,该管道对
comments
集合执行操作,并创建一个额外字段,返回管道步骤生成的数组中的项目计数

考虑运行以下聚合操作:

def posts_with_comment_count():
    pipeline = [
        { "$lookup": {
            "from": "comment",
            "localField": "_id",
            "foreignField": "post_id",
            "as": "comment_count"
        } },
        { "$addFields": {
            "comment_count": { "$size": "$comment_count" }
        } },
        { "$sort": { "_id": -1 } }
    ]
    posts = list(mongo.db.posts.aggregate(pipeline))

    return posts

非常感谢,我花了一点时间才意识到,我需要将您的代码传递到模板中,以代替以前对此类帖子的查询
return render\u template('home.html',posts=posts\u with\u comment\u count(),form=form,admin\u user=admin\u user())
非常感谢您,我花了一点时间才意识到我需要将您的代码传递到模板,以代替以前对此类帖子的查询<代码>返回呈现模板('home.html',posts=posts\u,带有注释\u count(),form=form,admin\u user=admin\u user())