【发布时间】:2020-08-19 09:56:51
【问题描述】:
如何在响应中仅显示有限的数据而不是整个对象
{
"message": "Records",
"data": [
{
"_id": "5f3ccf2448e5181dc80a70d3",
"string": "string",
"records": [
{
"_id": "5f3ccfc35417491564bbe2z9",
"product_id": "5ecd3a93f1314915404xd6ds",
"my_detail": {
"_id": "5ecc00f97c96f408384d57c4",
"active": 0,
"status": "pending",
"deleted_at": 0,
"email": "records@yopmail.com",
"password": "$2a$12$7h3mttR8TUlZZKIzQvnVV.1sdfsd56.sdfsdfBOxA6UBkpRBLFVC",
"first_name": "Johny",
"last_name": "Hales",
"createdAt": "2020-05-25T17:31:37.016Z",
"updatedAt": "2020-05-25T17:31:37.016Z",
}
},
{
"_id": "5f3ccfc35417491564bbe2z9",
"product_id": "5ecd3a93f1314915404xd6ds",
"my_detail": {
"_id": "5ecc00f97c96f408384d57c4",
"active": 0,
"status": "pending",
"deleted_at": 0,
"email": "records@yopmail.com",
"password": "$2a$12$7h3mttR8TUlZZKIzQvnVV.1sdfsd56.sdfsdfBOxA6UBkpRBLFVC",
"first_name": "Steve",
"last_name": "harvey",
"createdAt": "2020-05-25T17:31:37.016Z",
"updatedAt": "2020-05-25T17:31:37.016Z",
}
},
]
}
],
}
my_detail 应该只显示 my_detail 对象中的 first_name、last_name 和状态,而不是显示 _id、活动电子邮件、密码等所有内容,尝试不同的方式没有成功,请指导。
如何进行投影以显示有限的数据
$project : {
"string":1,
"records":1,
"records.my_detail.first_name":1,
"records.my_detail.last_name" :1,
"records.my_detail.status" :1
}
【问题讨论】:
标签: mongodb