【问题标题】:Cloudant Selector Query for fetching particular elements inside Array用于获取 Array 中特定元素的 Cloudant 选择器查询
【发布时间】:2017-08-02 12:27:06
【问题描述】:

我有以下要求。 在下面的数组元素中,我必须选择并比较 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 条记录。 请建议如何仅获取数组块中的特定元素。

【问题讨论】:

  • 对于返回的每条记录,您确定没有包含 3000 的 loadamout 的 subloans 元素吗? $elemMatch 不会返回匹配的数组,而是返回整个文档。
  • 有点。在这种情况下,OP 专门询问是否只返回数组中的特定元素。另一个帖子中也有人问过,但没有真正回答。

标签: json couchdb cloudant


【解决方案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"
               }
            }
         }
      }
   ]
}

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    相关资源
    最近更新 更多