2017-08-02 55 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