My goal is to add a comment to my CommentFeed and while doing that I want to push that comment into my topComments
field and also update the 'numOfComments' . I want to limit the topComments
to only 3 comments (How would I even set that up?). And how do I take the previous value of numOfComments
and add one to it?
CommentFeed.findOneAndUpdate(
{ _id: commentId },
{
$push: {
comments: {
text: req.body.text
},
$push: topComments:{text: req.body.text}, <--- Limit this somehow to only allow an array length of 3?
$set: numOfComments: ? , <---What kind of logic is used here?
}
},
{ new: true }
)
CommentFeed Schema
const CommentFeedSchema = new Schema({
topComments:[{text:{type:String}}],
numOfComments:{type:Number},
comments: [
text: { type: String, required: true }
]});