Cloudant Selector Query for fetching particular elements inside Array

放肆的年华 提交于 2020-01-24 22:59:08

问题


I have the below requirement. In the below array elements I have to select and compare the value of LoanAmount. In the previous posts, the below solutions are mentioned.

{
  "_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 query:

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

But that index and selector queries are providing all the records for that particular Query instead of providing me only record with 3000. Please suggest how to fetch only particular element inside an array block.


回答1:


I don't think it's possible to only return specific items in an array. You could accomplish something similar using views. Here is an example design document:

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

This creates a view called by_amount. Here is the map function:

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]});
    }
  }
}

Here I am using the loan amount as the key. This let's you query by the loan amount. The value can be whatever you want to return. In this case I am returning a document with the user's profile and the loan.

You can then query this view like so:

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

Which results in the something like the following (note: I added a second loan with a value of 3000 to show how it would look with multiple loans that matched):

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


来源:https://stackoverflow.com/questions/45460879/cloudant-selector-query-for-fetching-particular-elements-inside-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!