node.js - 如何在打开另一个本地字段之前解决一个本地字段中所有外国字段的mongodb聚合查找返回结果
问题描述
我搜索了互联网和 StackOverflow,但我找不到答案,甚至找不到问题。
我有两个系列,工匠和用户。我希望我的查询返回所有报告并指示指定用户是否将该报告作为其数组中的收藏夹。
我发现很难删除第一个结果中设置的下一个数据的另一个结果。我很想就如何从汇总语句中获得正确值获得一些帮助。
如果你仔细观察,你会发现 "_id": "5f21f998fe054d2cf0b7f787" 显示了 3 次,但正确的一个是工匠的 "user_id": "5f21f998fe054d2cf0b7f787"。其他人也一样
下面是代码和我得到的结果
谢谢
User.aggregate([
{
$lookup: {
from: Artisan.collection.name,
localField: "user._id",
foreignField: "artisan.user_id",
as: "artisan"
}
},
{$unwind: '$artisan'}
])
.then((result) => {
res.json(result);
}, (err) => next(err))
.catch((err) => next(err));```
\\Here is the result
[
{
"_id": "5f21f998fe054d2cf0b7f787",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Ayobami",
"lastname": "Eniola",
"email": "ekundayoolumide@gmail.com",
"password": "$2b$10$BlfwHzVgZ4kWYfG6Ia4x.Oj6cdVG24Cwl3R9FZQ9lK/1LoUg.WbAi",
"user_type": "artisan",
"createdAt": "2020-07-29T22:35:04.601Z",
"updatedAt": "2020-07-29T22:35:04.601Z",
"__v": 0,
"artisan": {
"_id": "5f21f998fe054d2cf0b7f788",
"date_of_birth": null,
"duration": "",
"payment_status": "",
"payment_type": "",
"user_id": "5f21f998fe054d2cf0b7f787",
"createdAt": "2020-07-29T22:35:04.645Z",
"updatedAt": "2020-07-29T22:35:04.645Z",
"__v": 0
}
},
{
"_id": "5f21f998fe054d2cf0b7f787",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Ayobami",
"lastname": "Eniola",
"email": "ekundayoolumide@gmail.com",
"password": "$2b$10$BlfwHzVgZ4kWYfG6Ia4x.Oj6cdVG24Cwl3R9FZQ9lK/1LoUg.WbAi",
"user_type": "artisan",
"createdAt": "2020-07-29T22:35:04.601Z",
"updatedAt": "2020-07-29T22:35:04.601Z",
"__v": 0,
"artisan": {
"_id": "5f21fa632fc70100d0339194",
"date_of_birth": null,
"duration": "4 years",
"payment_status": "",
"payment_type": "",
"user_id": "5f21fa632fc70100d0339193",
"createdAt": "2020-07-29T22:38:27.998Z",
"updatedAt": "2020-07-29T22:42:22.601Z",
"__v": 0
}
},
{
"_id": "5f21f998fe054d2cf0b7f787",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Ayobami",
"lastname": "Eniola",
"email": "ekundayoolumide@gmail.com",
"password": "$2b$10$BlfwHzVgZ4kWYfG6Ia4x.Oj6cdVG24Cwl3R9FZQ9lK/1LoUg.WbAi",
"user_type": "artisan",
"createdAt": "2020-07-29T22:35:04.601Z",
"updatedAt": "2020-07-29T22:35:04.601Z",
"__v": 0,
"artisan": {
"_id": "5f227b0433105d3a3864547e",
"date_of_birth": null,
"duration": "",
"payment_status": "",
"payment_type": "",
"user_id": "5f227b0433105d3a3864547d",
"createdAt": "2020-07-30T07:47:16.269Z",
"updatedAt": "2020-07-30T07:47:16.269Z",
"__v": 0
}
},
{
"_id": "5f21fa632fc70100d0339193",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Akinmade",
"lastname": "Eniola",
"email": "ekundayoolumide1@gmail.com",
"password": "john1234",
"user_type": "artisan",
"createdAt": "2020-07-29T22:38:27.957Z",
"updatedAt": "2020-07-29T22:42:22.454Z",
"__v": 0,
"artisan": {
"_id": "5f21f998fe054d2cf0b7f788",
"date_of_birth": null,
"duration": "",
"payment_status": "",
"payment_type": "",
"user_id": "5f21f998fe054d2cf0b7f787",
"createdAt": "2020-07-29T22:35:04.645Z",
"updatedAt": "2020-07-29T22:35:04.645Z",
"__v": 0
}
},
{
"_id": "5f21fa632fc70100d0339193",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Akinmade",
"lastname": "Eniola",
"email": "ekundayoolumide1@gmail.com",
"password": "john1234",
"user_type": "artisan",
"createdAt": "2020-07-29T22:38:27.957Z",
"updatedAt": "2020-07-29T22:42:22.454Z",
"__v": 0,
"artisan": {
"_id": "5f21fa632fc70100d0339194",
"date_of_birth": null,
"duration": "4 years",
"payment_status": "",
"payment_type": "",
"user_id": "5f21fa632fc70100d0339193",
"createdAt": "2020-07-29T22:38:27.998Z",
"updatedAt": "2020-07-29T22:42:22.601Z",
"__v": 0
}
},
{
"_id": "5f21fa632fc70100d0339193",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Akinmade",
"lastname": "Eniola",
"email": "ekundayoolumide1@gmail.com",
"password": "john1234",
"user_type": "artisan",
"createdAt": "2020-07-29T22:38:27.957Z",
"updatedAt": "2020-07-29T22:42:22.454Z",
"__v": 0,
"artisan": {
"_id": "5f227b0433105d3a3864547e",
"date_of_birth": null,
"duration": "",
"payment_status": "",
"payment_type": "",
"user_id": "5f227b0433105d3a3864547d",
"createdAt": "2020-07-30T07:47:16.269Z",
"updatedAt": "2020-07-30T07:47:16.269Z",
"__v": 0
}
},
{
"_id": "5f227b0433105d3a3864547d",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Akinmade",
"lastname": "Eniola",
"email": "ekundayoolumide2@gmail.com",
"password": "$2b$10$jg/nKNpg/RkDA.KFG/flFuy8fwfamLdgCD7MNk16rprDiwoWTKcse",
"user_type": "artisan",
"createdAt": "2020-07-30T07:47:16.234Z",
"updatedAt": "2020-07-30T07:47:16.234Z",
"__v": 0,
"artisan": {
"_id": "5f21f998fe054d2cf0b7f788",
"date_of_birth": null,
"duration": "",
"payment_status": "",
"payment_type": "",
"user_id": "5f21f998fe054d2cf0b7f787",
"createdAt": "2020-07-29T22:35:04.645Z",
"updatedAt": "2020-07-29T22:35:04.645Z",
"__v": 0
}
},
{
"_id": "5f227b0433105d3a3864547d",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Akinmade",
"lastname": "Eniola",
"email": "ekundayoolumide2@gmail.com",
"password": "$2b$10$jg/nKNpg/RkDA.KFG/flFuy8fwfamLdgCD7MNk16rprDiwoWTKcse",
"user_type": "artisan",
"createdAt": "2020-07-30T07:47:16.234Z",
"updatedAt": "2020-07-30T07:47:16.234Z",
"__v": 0,
"artisan": {
"_id": "5f21fa632fc70100d0339194",
"date_of_birth": null,
"duration": "4 years",
"payment_status": "",
"payment_type": "",
"user_id": "5f21fa632fc70100d0339193",
"createdAt": "2020-07-29T22:38:27.998Z",
"updatedAt": "2020-07-29T22:42:22.601Z",
"__v": 0
}
},
{
"_id": "5f227b0433105d3a3864547d",
"email_verified": null,
"sex": "male",
"phone_no": 2348117741341,
"address": "10 Oweh Street",
"area": "",
"city": "Lagos",
"state": "Lagos",
"country": "",
"job_type": "great work",
"status": "",
"login_key": "",
"login_valid_till": null,
"firstname": "Johnson",
"othername": "Akinmade",
"lastname": "Eniola",
"email": "ekundayoolumide2@gmail.com",
"password": "$2b$10$jg/nKNpg/RkDA.KFG/flFuy8fwfamLdgCD7MNk16rprDiwoWTKcse",
"user_type": "artisan",
"createdAt": "2020-07-30T07:47:16.234Z",
"updatedAt": "2020-07-30T07:47:16.234Z",
"__v": 0,
"artisan": {
"_id": "5f227b0433105d3a3864547e",
"date_of_birth": null,
"duration": "",
"payment_status": "",
"payment_type": "",
"user_id": "5f227b0433105d3a3864547d",
"createdAt": "2020-07-30T07:47:16.269Z",
"updatedAt": "2020-07-30T07:47:16.269Z",
"__v": 0
}
}
]
解决方案
现在你有:
$lookup: {
from: Artisan.collection.name,
localField: "user._id",
foreignField: "artisan.user_id",
as: "artisan"
}
查看结果后,User
集合中的文档没有名为 的字段user._id
,它们只有一个_id
字段。与您的集合类似,Artisan
没有名为 的字段artist.user_id
,只有一个user_id
字段。localField
将和值更改foreignField
为仅具有出现在相应文档中的字段名称。
它应该看起来像这样:
$lookup: {
from: Artisan.collection.name,
localField: "_id",
foreignField: "user_id",
as: "artisan"
}
推荐阅读
- ajax - 在 XHR 缓冲区上发送 Struts 标记
- django - 循环使用返回查询的参数的自定义标记
- git - 多个特性并行开发,git一次合并
- php - 如何通过 artisan 执行或测试 laravel $schedule->call 函数
- java - 在 Android 的片段中创建可滚动的网格视图
- javascript - 如何修改此脚本以允许输入另一个字符?
- php - PHP 检查数组是否以特定值开始并以特定值结束,
- angularjs - AngularJS - 不知道如何用 ng-repeat、逻辑问题来实现这个功能
- asp.net-mvc - 输入字符串的格式不正确 - MediaInfoLib
- rest - Rest Accept 标头逗号与分号