我有一個評論集合和一個帖子集合,帖子架構是這樣的:
postSchema.virtual("comments", {
ref: "Comment",
localField: "_id",
foreignField: "post",
})
當我想查看帖子的評論時,我使用這個:
const post = await Post.findById(req.params.id);
await post.populate("comments");
res.send(post.comments);
我不想獲得 post.comments 的電子郵件欄位。我嘗試了 foreach 洗掉鍵,但它沒有幫助我 tnx
uj5u.com熱心網友回復:
您也可以對虛擬物件使用常規欄位選擇方法.populate:
await post.populate("comments", "-email");
// or if you want to exclude more than one field:
// await post.populate("comments", "-email -field2 -field3");
如果要使用正選擇(結果中應包含哪些欄位),請確保始終包含foreignField虛擬的 (在這種情況下post):
await post.populate("comments", "post field2 field3");
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497631.html
標籤:javascript 节点.js 表示 猫鼬
