Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算不同于父模式的mongodb模式?_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 如何计算不同于父模式的mongodb模式?

Javascript 如何计算不同于父模式的mongodb模式?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,目前我有一个小问题。我有一个模式,存储我的博客文章。在另一个模式中,我保存注释。在这个模式中,我保存了博客文章中的parent.id 现在,我想计算评论的数量,在博客帖子下面显示这个数量。 (信息:我用的是express,edge。所有人都认为效果很好!) 我的模块看起来像: const Post = require('../database/models/Post'); const Comment = require('../database/models/Comment'); modul

目前我有一个小问题。我有一个模式,存储我的博客文章。在另一个模式中,我保存注释。在这个模式中,我保存了博客文章中的parent.id

现在,我想计算评论的数量,在博客帖子下面显示这个数量。 (信息:我用的是express,edge。所有人都认为效果很好!)

我的模块看起来像:

const Post = require('../database/models/Post');
const Comment = require('../database/models/Comment');


module.exports = async(req, res) => {
  const posts = await Post.find({}).sort({ _id: -1 });
  const estimate = await Post.count({}); //all post blogs  
  const comments = await Comment.count({}); //all comments

// here I want to count all comments from one post blog ... etc ...

res.render("index", {
    posts, estimate, comments
  });
}
以下是模式:


const mongoose = require('mongoose');
 
const PostSchema = new mongoose.Schema({
    title: String,
    description: String,
    content: String,
    username: String,
    createdAt: {
        type: Date,
        default: new Date()
    }
});
 
const Post = mongoose.model('Post', PostSchema);
 
module.exports = Post;

--------

const mongoose = require('mongoose');
 
const CommentSchema = new mongoose.Schema({
    comment: {
        type: String,
        //required: true
    },
    username: {
        type: String,
    },
    parent_id: {        // <-- the _id from posts
        type: String
    }
});

var Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;


嗯。。。我不知道出了什么问题。

我不确定你是想要某个特定帖子的评论数还是所有帖子的评论数,所以让我们回顾一下每个案例

一篇文章的评论计数 这可以通过简单地在count函数中添加一个查询来完成,该查询指定blog id

const commentCount = await Post.count({ parent_id: blog_id });
所有帖子的评论计数 这个有点复杂。您将需要使用Mongo聚合(您可以阅读更多关于它们的信息)。基本上,您希望按
父id
对您的
评论进行分组并对其进行计数,因此下面是它的外观:

const allPostCommentCount = await Comment.aggregate({
  $group: {
    _id: "$parent_id",
    count: {
      $sum: 1,
    },
  },
});

第二个是我需要的。但是我怎样才能将这个结果放入我的edge框架中,以显示每个主要博客文章下的计数?我没有使用edge的经验,但基本上结果是一个数组,每个对象都有以下格式:
{u id:24,count:1}
因此,基本上,当你浏览你的博客文章时,你只需在评论计数列表中找到与特定id匹配的条目,并显示
计数
param.res.render(“index”,{posts,estimate,commentcount});
const commentCount = await Post.count({ parent_id: blog_id });
const allPostCommentCount = await Comment.aggregate({
  $group: {
    _id: "$parent_id",
    count: {
      $sum: 1,
    },
  },
});