我只想显示一个带有那些评论(它是一个数组)的单个文章,这些评论在我的数据库中的批准值设置为“ true”。我无法得到正确的结果。这是我的后端代码。我使用纯MongoDB,没有Mongoose。
app.get("/posts/:id", function (req,res) {
blog.collection("posts").findOne({"_id":ObjectId(req.params.id)}
,function (error,post )
{
res.render("user/post",{post:post});
});
});
这是我的数据库的外观,我想过滤或将结果限制为comment approve =“ true”。
非常感谢您的每一个回答。
我不知道是否有更好的解决方案,但是可以通过以下方法获得:
let newPost = {...post}
newPost.comments = newPost.comments.filter(comment=>comment.approve)
res.render("user/post",{post:newPost});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句