Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudant Selector Query

Tags:

json

cloudant

I would like to query using cloudant db using selector, for example that is shown below: user would like to have loanborrowed whose amount exceeds a number, how to access the array in a cloudant selector to find a specific record

{
       "_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"
          }
        }
      ]
    }
like image 977
vinSan Avatar asked Oct 21 '15 14:10

vinSan


People also ask

What type of queries does Cloudant support?

In databases, frequently used data and related queries are indexed to speed up queries. Cloudant supports two types of indexes: "type": "text" "type": "json"

What are the two types of Cloudant query indexes?

Cloudant Query has two types of index — json and text .

How do I access my Cloudant database?

You can access IBM Cloudant directly by using an HTTP client rather than an IBM Cloudant client library. However, you must handle exchanging and refreshing a time-limited access token by using an IAM API key with the IAM token service. After a token expires, IBM Cloudant returns an HTTP 401 status code.


1 Answers

If you use the default Cloudant Query index (type text, index everything):

{
   "index": {},
   "type": "text"
}

Then the following query selector should work to find e.g. all documents with a loanamount > 1000:

"loansBorrowed": { "$elemMatch": { "loanamount": { "$gt": 1000 } } }

I'm not sure that you can coax Cloudant Query to only index nested fields within an array so, if you don't need the flexibility of the "index everything" approach, you're probably better off creating a Cloudant Search index which indexes just the specific fields you need.

like image 195
Will Holley Avatar answered Oct 16 '22 04:10

Will Holley