I have a list of posts and each post contains an array of comments, each comment might be private or public and I want to show Admins all private and public comments but normal users I want to show them only public comments.
here is a part of the post and comment Schema:
const PostSchema = new mongoose.Schema({
title: String,
comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
})
const CommentSchema = new mongoose.Schema({
body: String,
type: { type: String, enum: ['public', 'private'] }
})
here is the solution I came with: Grab the post by id:
const post= await Post.findById(id);
and then filter:
post.comments = post.comments.filter(c => c.type != "private");
return res.json(post)
but I want to do it full mongoose if that's possible.
typetopublicand set the data type to boolean (true or false).