一尘不染

Cloudant选择器查询

json

我想使用选择器使用cloudant db进行查询,例如下面所示:用户希望借入的借贷金额超过数字,如何在cloudant选择器中访问数组以查找特定记录

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

阅读 313

收藏
2020-07-27

共1个答案

一尘不染

如果您使用默认的Cloudant Query索引(键入文本,对所有内容建立索引):

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

然后,以下查询选择器应可以查找例如所有借贷金额> 1000的文档:

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

我不确定您是否可以哄骗Cloudant查询仅索引数组中的嵌套字段,因此,如果您不需要“为所有内容建立索引”的灵活性,那么最好创建一个Cloudant
Search索引来索引只是您需要的特定字段。

2020-07-27