【发布时间】:2020-09-07 03:55:10
【问题描述】:
我希望下面的 sn-ps 能让我的问题更清楚。我可以获取一个嵌入了类别对象数组的对象。从类别对象中,我只想显示某些属性。
这就是我现在得到的:
{
"photo": [],
"_id": "5ec55ae39eere8ert85bf8fe749",
"title": "Blog about coding",
"description": "Hi, I want to share with you my incredible journey as a software developer...",
"rate": 3,
"category": [
{
"_id": "5ec55ae39aaa78985f8fe74a",
"name": "programming",
"display_name": "Programming"
}
],
"createdAt": "2020-05-20T16:29:23.982Z",
"updatedAt": "2020-05-20T16:29:23.982Z",
"__v": 0
}
使用以下代码:
/* GET blog with id */
router.get("/:id", async (req, res, next) => {
try {
const blog = await Blog.findById(req.params.id); // How to filter out fields of objects in category array?
res.send(blog);
} catch(ex) {
res.status(400).send(ex);
}
});
这是我真正想要的:
{
"photo": [],
"_id": "5ec55ae39eere8ert85bf8fe749",
"title": "Blog about coding",
"description": "Hi, I want to share with you my incredible journey as a software developer...",
"rate": 3,
"category": [
{
"display_name": "Programming"
}
],
"createdAt": "2020-05-20T16:29:23.982Z",
"updatedAt": "2020-05-20T16:29:23.982Z",
"__v": 0
}
所以,在类别中我只想要“display_name”
我怎样才能得到这个。我以为我以前看到过解决方案,但现在找不到了。
【问题讨论】:
标签: node.js mongodb express mongoose mongodb-query