Cloudant Selector Query用于获取Array

时间:2017-08-02 12:27:06

标签: json couchdb cloudant

我有以下要求。 在下面的数组元素中,我必须选择并比较LoanAmount的值。在之前的帖子中,提到了以下解决方案。

{
  "_id": "65c5e4c917781f7365f4d814f6e1665f",
  "_rev": "2-73615006996721fef9507c2d1dacd184",
  "userprofile": {
            "name": "tom",
            "age": 30,
            "employer": "Microsoft"            
                 },
  "loansBorrowed": [{"loanamount": 5000,
                     "loandate": "01/01/2001",
                     "repaymentdate": "01/01/2001",
                     "rateofinterest": 5.6,
                     "activeStatus": true,
                     "penalty": {
                        "penalty-amount": 500,
                        "reasonforPenalty": "Exceeded the date by 10 days"        
                     }
                    },
                    {
                      "loanamount": 3000,
                      "loandate": "01/01/2001",
                      "repaymentdate": "01/01/2001",
                      "rateofinterest": 5.6,
                      "activeStatus": true,
                      "penalty": {
                        "penalty-amount": 400,
                        "reasonforPenalty": "Exceeded the date by 10 days"
                      }
                    },
                    {
                      "loanamount": 2000,
                      "loandate": "01/01/2001",
                      "repaymentdate": "01/01/2001",
                      "rateofinterest": 5.6,
                      "activeStatus": true,
                      "penalty": {
                        "penalty-amount": 500,
                        "reasonforPenalty": "Exceeded the date by 10 days"
                      }
                    }
                  ]
                }

Index:
     { 
      "index": {
                "fields": [{
                    "name": "loansBorrowed.[].loanamount",
                    "type":"number"            
                }],            
                "type": "json"
            }

选择器查询:

{"selector": {
        "loansBorrowed": {
            "$elemMatch": {
                "loanamount": 3000
            }
        }
    }
}

但是索引和选择器查询正在为该特定查询提供所有记录,而不是仅为我提供3000记录。 请建议如何仅获取数组块中的特定元素。

1 个答案:

答案 0 :(得分:1)

我认为只能返回数组中的特定项目。您可以使用视图完成类似的操作。这是一个示例设计文档:

{
  "_id": "_design/loans",
  "_rev": "1-a115abe01632dd43ee1d0d10546b737d",
  "views": {
    "by_amount": {
      "map": "function (doc) {\n  if (doc.loansBorrowed) {\n    for (var i=0; i<doc.loansBorrowed.length; i++) {\n      emit(doc.loansBorrowed[i].loanamount, {userprofile: doc.userprofile, loan:doc.loansBorrowed[i]});\n    }\n  }\n}"
    }
  },
  "language": "javascript"
}

这将创建一个名为by_amount的视图。这是地图功能:

function (doc) {
  if (doc.loansBorrowed) {
    for (var i=0; i<doc.loansBorrowed.length; i++) {
      emit(doc.loansBorrowed[i].loanamount, {userprofile: doc.userprofile, loan:doc.loansBorrowed[i]});
    }
  }
}

这里我使用贷款金额作为关键。这让你用贷款金额来查询。值可以是您想要返回的任何值。在这种情况下,我将返回一份包含用户个人资料和贷款的文件。

然后您可以像这样查询此视图:

https://xxx.cloudant.com/YOUR_DB/_design/loans/_view/by_amount?key=3000

这导致类似下面的内容(注意:我添加了第二笔贷款,其价值为3000,以显示与多个相匹配的贷款的外观):

{
   "total_rows":6,
   "offset":2,
   "rows":[
      {
         "id":"796a8954600cee9dbb9e0a4040593942",
         "key":3000,
         "value":{
            "userprofile":{
               "name":"tom",
               "age":30,
               "employer":"Microsoft"
            },
            "loan":{
               "loanamount":3000,
               "loandate":"01/01/2001",
               "repaymentdate":"01/01/2001",
               "rateofinterest":5.6,
               "activeStatus":true,
               "penalty":{
                  "penalty-amount":400,
                  "reasonforPenalty":"Exceeded the date by 10 days"
               }
            }
         }
      },
      {
         "id":"c93f52da36a51f0ddd75f5be381c916e",
         "key":3000,
         "value":{
            "userprofile":{
               "name":"joe",
               "age":50,
               "employer":"Google"
            },
            "loan":{
               "loanamount":3000,
               "loandate":"01/01/2001",
               "repaymentdate":"01/01/2001",
               "rateofinterest":5.6,
               "activeStatus":true,
               "penalty":{
                  "penalty-amount":400,
                  "reasonforPenalty":"Exceeded the date by 10 days"
               }
            }
         }
      }
   ]
}