简体   繁体   English

Java-通过多个属性过滤JSON对象数组

[英]Java - Filtering an array of JSON objects by multiple properties

I have an object stored in Cloudant like this : 我有一个像这样存储在Cloudant中的对象:

{
  "attr1": "value",
  "Objects": [{
      "code": "v1",
      "allowNull": "true",
      "country": "BE"
    },
    {
      "code": "v2",
      "allowNull": "false",
      "country": "EG"
    }
  ]
}

And I want to do a filter criteria by code/country, so the output would be only one object of Objects list. 而且我想按代码/国家/地区做一个过滤条件,因此输出将只是“对象”列表的一个对象。

Is there a way to do that from Cloudant side? 有办法从Cloudant方面做到这一点吗? Or an efficient way to be done at Java side ? 还是在Java方面可以完成的有效方法?

You can achieve this with a map-reduce view in Cloudant. 您可以使用Cloudant中的map-reduce视图来实现。 Try something along the lines of this: 尝试一些类似的方法:

function(doc) {
    if (doc && doc.Objects) {
        doc.Objects.forEach(function(obj) {
            emit([obj.code, obj.country], obj);
        });
    }
}

This emits all items in the Objects list into the index, with a vector-valued key [code, country] . 这会使用向量值键[code, country]将“ Objects列表中的所有项目发出到索引中。

curl -s -XPOST -H "content-type:application/json" 'https://skruger.cloudant.com/demo/_design/query/_view/by_code_country?reduce=false' -d '{ "keys": [["v1","BE"]] }'
{"total_rows":2,"offset":0,"rows":[
    {"id":"1c593a931bcd7f0052ed8f9184810fd9","key":["v1","BE"],"value": 
        {"code":"v1","allowNull":"true","country":"BE"}}
]}

You can query by code only, using the {} wildcard marker, eg 您只能使用{}通配符标记按代码查询,例如

curl -g 'https://skruger.cloudant.com/demo/_design/query/_view/by_code_country?reduce=false&startkey=["v1"]&endkey=["v1",{}]'
{"total_rows":2,"offset":0,"rows":[
    {"id":"1c593a931bcd7f0052ed8f9184810fd9","key":["v1","BE"],"value": 
    {"code":"v1","allowNull":"true","country":"BE"}}
]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM