0

const commentSchema = new mongoose.Schema({
  name: {
    type: String,
    default: "anonymous",
  },
  comment: {
    type: String,
    required: true,
  },
  post: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Post",
    required: true,
  },
}, {
  timestamps: true
});

const trending = await Comment.aggregate([{
  $group: {
    _id: "$post",
    total: {
      $sum: 1
    },
  },
}, ]);

Result of aggregation
[{
    "_id": "61eb55808551961dc737c00c",
    "total": 5
  },
  {
    "_id": "61eb4a490a894ac62bd833ab",
    "total": 2
  }
]

hello, please is there a way i can populate the field _id after using aggregation, i want to get all the fields of post not just the _id?

john otor
  • 49
  • 7
  • Does this answer your question? [how to use populate and aggregate in same statement?](https://stackoverflow.com/questions/16680015/how-to-use-populate-and-aggregate-in-same-statement) – eol Jan 22 '22 at 09:43
  • i don't really understand the answer, this part Patients.populate(result, {path: "patient"}, callback); – john otor Jan 22 '22 at 09:51

0 Answers0