首页 > 解决方案 > 如何在打开另一个本地字段之前解决一个本地字段中所有外国字段的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
      }
  }
]

标签: node.jsmongodbaggregation-framework

解决方案


现在你有:

$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"
}

推荐阅读