2017-08-02 32 views
1

我有以下要求。 在下面的数组元素中,我必须选择并比较LoanAmount的值。在之前的文章中,提到了下面的解决方案。Cloudant Selector查询获取阵列中的特定元素

{ 
    "_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 记录的所有记录,请建议如何只获取特定元素在一个数组块内。

+0

对于每个返回的记录,您确定没有subloans元素包含3000的loadamout? $ elemMatch不会返回匹配的数组,它返回整个文档。 –

+1

一个非常精确的dupe:https://stackoverflow.com/questions/33262573/cloudant-selector-query – xpqz

+0

类别。在这种情况下,OP特别要求仅返回数组中的特定元素。另一篇文章也提到了这个问题,但没有真正回答。 – markwatsonatx

回答

0

我不认为有可能只返回数组中的特定项目。你可以使用视图来完成类似的事情。下面是一个设计文档示例:

{ 
    "_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" 
       } 
      } 
     } 
     } 
    ] 
} 
+0

感谢您的解决方案!!它的工作! – Divya