Cloudant Selector Query
Asked Answered
S

2

11

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"
          }
        }
      ]
    }
Sienese answered 21/10, 2015 at 14:51 Comment(0)
T
13

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.

Teddy answered 21/10, 2015 at 20:52 Comment(0)
E
10

While Will's answer works, I wanted to let you know that you have other indexing options with Cloudant Query for handling arrays. This blog has the details on various tradeoffs (https://cloudant.com/blog/mango-json-vs-text-indexes/), but long story short, I think this might be the best indexing option for you:

{
  "index": {
    "fields": [
      {"name": "loansBorrowed.[].loanamount", "type": "number"}
    ]
  },
  "type": "text"
}

Unlike Will's index-everything approach, here you're only indexing a specific field, and if the field contains an array, you're also indexing every element in the array. Particularly for "type": "text" indexes on large datasets, specifying a field to index will save you index-build time and storage space. Note that text indexes that specify a field must use the following form in the "fields": field: {"name": "fieldname", "type": "boolean,number, or string"}

So then the corresponding Cloudant Query "selector": statement would be this:

{
  "selector": {
    "loansBorrowed": {"$elemMatch": {"loanamount": {"$gt": 4000}}}
  },
  "fields": [
    "_id",
    "userprofile.name",
    "loansBorrowed"
  ]
}

Also note that you don't have to include "fields": as part of your "selector": statement, but I did here to only project certain parts of the JSON. If you omit it from your "selector": statement, the entire document will be returned.

Essy answered 20/11, 2015 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.