首页 > 解决方案 > 一对多关系

问题描述

我有以下情况,不知道如何解决。我尝试了不同的方式,但没有成功。我什至不知道这是否可能。我希望使用带有映射(或减少)功能的 CouchDB 视图来完成此操作。我想按照以下示例在 CouchDB 中实现一对多关系:

{
      "_id": 1,
      "name" : "Me",
      "doctype" : "employee"
    },
    {
     "_id" : 2,
     "name" : "MeAgain",
     "doctype" : "employee"
    }, 
    {
     "_id" : 3,
     "doctype": "holidayrequest",
     "startdate": "01/25/2021",
     "requesterid": "1"
    },
    {
     "_id" : 4,
     "doctype": "holidayrequest",
     "startdate": "01/16/2021",
     "requesterid": "1"
    },
    {
     "_id" : 5,
     "doctype": "holidayrequest",
     "startdate": "04/16/2021",
     "requesterid": "2"
    },
    {
     "_id" : 6,
     "doctype": "holidayrequest",
     "startdate": "04/25/2021",
     "requesterid": "2"
    }

我希望结果是:

{
 "_id": 1,
  "name" : "Me",
  "doctype" : "employee",
  "holidays":[
     {
        "_id" : 3,
        "doctype": "holidayrequest",
        "startdate": "01/25/2021",
        "requesterid": "1"
     },
     {
        "_id" : 4,
        "doctype": "holidayrequest",
        "startdate": "01/16/2021",
        "requesterid": "1"
     }
  ]
},
{
 "_id" : 2,
 "name" : "MeAgain",
 "doctype" : "employee",
 "holidays":[
     {
      "_id" : 5,
      "doctype": "holidayrequest",
      "startdate": "04/16/2021",
      "requesterid": "2"
    },
    {
      "_id" : 6,
      "doctype": "holidayrequest",
      "startdate": "04/25/2021",
      "requesterid": "2"
    }
 ] 
}

这可以做到吗?

标签: node.jscouchdb

解决方案


推荐阅读