在单个 n1ql 查询中从所选文档获取所有相关文档和聚合数据

Getting all the related doc and aggregate data from selected doc in single n1ql query

我刚开始使用 couchbase,我有以下要求

我有以下文档:

推荐文件

{
    "_id": "8c658e34-34cc-4e61-8b26-5abc1cebd95e",
  "_metadata": {
    "created_at": 1472550145662,
    "created_by": "api-tenant-1"
  },
  "_type": "UWTrans",
  "amount": 4,
  "reference": {
    "refTranId": "0c0133f4-ade7-4190-8a43-a59de43846b4",
    "refTranModule": "NSP",
    "refTranType": "BUY"
  },
  "source": "WALLET",
  "tranType": "REFERRAL",
  "userId": "BJsMyG57c18f13"
} 

现在我需要拥有特定用户 ID 的所有推荐文件,我还想要这样的所有推荐金额的总和

[
  {
     "totalAmountEarned": 16,
     "transactions": [{
         "_id": "8c658e34-34cc-4e61-8b26-5abc1cebd95e",
         "_metadata": {
             "created_at": 1472550145662,
             "created_by": "api-tenant-1"
         },
         "_type": "UWTrans",
         "amount": 4,
         "reference": {
             "refTranId": "0c0133f4-ade7-4190-8a43-a59de43846b4",
             "refTranModule": "NSP",
            "refTranType": "BUY"
         },
        "source": "WALLET",
        "tranType": "REFERRAL",
        "userId": "BJsMyG1s57c18f13"
     },{
         "_id": "8c658e34-34cc-4e61-8b26-5abc1cebd95e",
         "_metadata": {
             "created_at": 1472550145662,
             "created_by": "api-tenant-1"
         },
         "_type": "UWTrans",
         "amount": 12,
         "reference": {
             "refTranId": "r58133f4-fd8-41h0-82t3-a59b783er6b4",
             "refTranModule": "URP",
             "refTranType": "SOLD"
         },
        "source": "WALLET",
        "tranType": "REFERRAL",
        "userId": "BJsMyG1s57c18f13"
     }]
  }
]

我想要这样或类似的输出。我试过 nest 但没有得到想要的输出

提前致谢

试试这个查询

  SELECT sum(amount) AS totalAmountEarned, array_agg(d) AS transactions
    FROM default d WHERE _type = "UWTrans" AND userId = "BJsMyG57c18f13";
    [
      {
        "totalAmountEarned": 16,
        "transactions": [
          {
            "_id": "8c658e34-34cc-4e61-8b26-5abc1cebd95e",
            "_metadata": {
              "created_at": 1472550145662,
              "created_by": "api-tenant-1"
            },
            "_type": "UWTrans",
            "amount": 4,
            "reference": {
              "refTranId": "0c0133f4-ade7-4190-8a43-a59de43846b4",
              "refTranModule": "NSP",
              "refTranType": "BUY"
            },
            "source": "WALLET",
            "tranType": "REFERRAL",
            "userId": "BJsMyG57c18f13"
          },
          {
            "_id": "8c658e34-34cc-4e61-8b26-5abc1cebd95e",
            "_metadata": {
              "created_at": 1472550145662,
              "created_by": "api-tenant-1"
            },
            "_type": "UWTrans",
            "amount": 12,
            "reference": {
              "refTranId": "0c0133f4-ade7-4190-8a43-a59de43846b4",
              "refTranModule": "NSP",
              "refTranType": "BUY"
            },
            "source": "WALLET",
            "tranType": "REFERRAL",
            "userId": "BJsMyG57c18f13"
          }
        ]
      }
    ]