如何在mongoose中获取嵌入对象并使其成为主要对象?



我有一个模型,它跟踪哪个用户追随了哪个作者。在端点中,我需要为作者的追随者数组提供服务。为此,我对传递author进行过滤的数据库进行了查询,并填充了follower字段。数据看起来像这样:

[
{
"follower": {
"_id": ""
"name": "",
"profilePicture": "",
"phone": "",
"email": ""
},
"author": "6137378438a6eb01a6a669ab",
"_id": "6131fb2e486dd00c88c99ed5"
},
{
"follower": {
"_id": ""
"name": "",
"profilePicture": "",
"phone": "",
"email": ""
},
"author": "6137378438a6eb01a6a669ab",
"_id": "6131fb2e486dd00c88c99ed5"
}
]

但是我只需要追随者的数据。我如何将追随者的对象设置为make对象,使上面的结果将是追随者对象的数组呢?

使用项目和地图

await Model.find({}, { follower: 1 }).map(f => f.follower);

最新更新