Return 某些字段带有来自 Mongoose 的 populate()
Return certain fields with populate() from Mongoose
在我 运行 此查询查询后,我从 MongoDB 返回了一个 JSON 值。
replyID 是另一个 Schema User 的对象,它有 _id、email 和 user。
但是 我不想要用户的电子邮件和 _id,只想要名字。
我应该向填充函数添加什么以仅获取名称?
查询 ->
.populate({
path: 'replies',
populate: { path: 'replyID' }
})
JSON 返回
"comments": [
{
"_id": "6296fcda39d113539bbe023d",
"creator": "62948aff05bcfdb5e75df7e6",
"postId": "62964a997c183d6c09bac126",
"comment": "Sebastian Goattel",
"likes": [
"62948aff05bcfdb5e75df7e6",
"62948f76727f0c0286949c7a"
],
"replies": [
{
"_id": "629733dcc3220cffa7cb2049",
"replyID": {
"_id": "62948f76727f0c0286949c7a",
"name": "Lorem Ipsum",
"email": "lorem@gmail.com",
}
"commentID": "6296fcda39d113539bbe023d",
"reply": "Lorem Ipsum 1",
},
{
"_id": "629733edc3220cffa7cb204f",
"replyID": "62948f76727f0c0286949c7a",
"commentID": "6296fcda39d113539bbe023d",
"reply": "Lorem Ipsum 2",
}
],
},
],
试试这个
.populate({
path: 'replies',
populate: { path: 'replyID', select: 'name -_id' }
})
在我 运行 此查询查询后,我从 MongoDB 返回了一个 JSON 值。 replyID 是另一个 Schema User 的对象,它有 _id、email 和 user。 但是 我不想要用户的电子邮件和 _id,只想要名字。 我应该向填充函数添加什么以仅获取名称? 查询 ->
.populate({
path: 'replies',
populate: { path: 'replyID' }
})
JSON 返回
"comments": [
{
"_id": "6296fcda39d113539bbe023d",
"creator": "62948aff05bcfdb5e75df7e6",
"postId": "62964a997c183d6c09bac126",
"comment": "Sebastian Goattel",
"likes": [
"62948aff05bcfdb5e75df7e6",
"62948f76727f0c0286949c7a"
],
"replies": [
{
"_id": "629733dcc3220cffa7cb2049",
"replyID": {
"_id": "62948f76727f0c0286949c7a",
"name": "Lorem Ipsum",
"email": "lorem@gmail.com",
}
"commentID": "6296fcda39d113539bbe023d",
"reply": "Lorem Ipsum 1",
},
{
"_id": "629733edc3220cffa7cb204f",
"replyID": "62948f76727f0c0286949c7a",
"commentID": "6296fcda39d113539bbe023d",
"reply": "Lorem Ipsum 2",
}
],
},
],
试试这个
.populate({
path: 'replies',
populate: { path: 'replyID', select: 'name -_id' }
})