Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中通过mongoTemplate查询,通过对象键值的嵌套数组获取mongoDB对象_Java_Mongodb_Spring Boot - Fatal编程技术网

如何在java中通过mongoTemplate查询,通过对象键值的嵌套数组获取mongoDB对象

如何在java中通过mongoTemplate查询,通过对象键值的嵌套数组获取mongoDB对象,java,mongodb,spring-boot,Java,Mongodb,Spring Boot,单个文档具有属性Id和值的属性数组。 我希望通过使用多个操作符匹配属性Id和数组中每个属性对象对应的值来检索对象。 问题:在执行Search GET API时,任何具有正在搜索的值的属性id都会被拾取,而不是特定的属性id,该属性id应该是唯一拾取的响应。 由于具有相同的属性值而拾取交叉引用的属性ID是问题的根本原因 下面是我的MongoDB JSON对象 { "_id" : ObjectId("5e3a6db03efb0c370c08c984"), "fil

单个文档具有属性Id和值的属性数组。 我希望通过使用多个操作符匹配属性Id和数组中每个属性对象对应的值来检索对象。 问题:在执行Search GET API时,任何具有正在搜索的值的属性id都会被拾取,而不是特定的属性id,该属性id应该是唯一拾取的响应。 由于具有相同的属性值而拾取交叉引用的属性ID是问题的根本原因

下面是我的MongoDB JSON对象

  {
        "_id" : ObjectId("5e3a6db03efb0c370c08c984"),
        "file" : {
            "filePath" : "https://file path",
            "size" : "16KB",
            "type" : "pdf"
        },
        "filePageCount" : 1,
        "receivedDate" : ISODate("2019-01-19T18:30:00.000Z"),
        "documentTypeId" : "5e2c4d606c1ac06b776b9ac8",
        "versionNumber" : 1,
        "attributes" : [ 
            {
                "attributeId" : "5e2c49a46c1ac06b776b9968",
                "value" : NumberLong(12345)
            }, 
            {
                "attributeId" : "5e2c49a66c1ac06b776b9969",
                "value" : NumberLong(67890)
            }, 
            {
                "attributeId" : "5e2c49af6c1ac06b776b996f",
                "value" : "app999"
            }, 
            {
                "attributeId" : "5e2c49ad6c1ac06b776b996e",
                "value" : "amount888"
            }, 
            {
                "attributeId" : "5e2c4a846c1ac06b776b99f0",
                "value" : ISODate("2020-02-02T00:00:00.000Z")
            }, 
            {
                "attributeId" : "5e2c4a856c1ac06b776b99f1",
                "value" : ISODate("2020-03-03T00:00:00.000Z")
            }
        ],
        "_class" : "com.uacc.drs.entity.Documents"
    }`enter code here`
尝试了多种方法

List<Documents> resultDocument = new ArrayList<Documents>();

        // to save list of criteria
        ArrayList<Criteria> c = new ArrayList<Criteria>();
        Query query = new Query();
        query.fields().include("_id");
        query.fields().include("documentTypeId");
        query.fields().include("attributes");


String[] values = queries.split("\\|");
Object data = dataTypeConvertion(values[0], values[2]);


switch (values[1]) {
                case "equalTo":
                    logger.debug("attributes values if equalTo");
                    Criteria c1 = Criteria.where("attributes.attributeId").is(values[0])
                            .andOperator(Criteria.where("attributes.value").is(data));
                    c.add(c1);
                    break;
                case "lessThan":
                    logger.debug("attributes values if lessThan");
                    Criteria c2 = Criteria.where("attributes.attributeId").is(values[0])
                            .andOperator(Criteria.where("attributes.value").lt(data));
                    c.add(c2);
                    break;
                case "greaterThan":
                    logger.debug("attributes values if greaterThan");
                    Criteria c3 = Criteria.where("attributes.attributeId").is(values[0])
                            .andOperator(Criteria.where("attributes.value").gt(data));
                    c.add(c3);
                    break;
                }

            query.addCriteria(new Criteria().andOperator(c.toArray(new Criteria[c.size()])));


        // get documents based on the query criteria
        List<Documents> documents = mongoTemplate.find(query, Documents.class);
List resultDocument=new ArrayList();
//保存条件列表的步骤
ArrayList c=新的ArrayList();
查询=新查询();
query.fields()包括(“_id”);
query.fields().include(“documentTypeId”);
query.fields().include(“属性”);
String[]values=querys.split(“\\\\”);
对象数据=数据类型转换(值[0],值[2]);
开关(值[1]){
案例“equalTo”:
logger.debug(“属性值如果相等”);
Criteria c1=Criteria.where(“attributes.attributeId”)是(值[0])
.andOperator(标准,其中(“attributes.value”)是(数据));
c、 添加(c1);
打破
“lessThan”案:
debug(“属性值如果小于此值”);
Criteria c2=Criteria.where(“attributes.attributeId”).is(值[0])
.andOperator(标准,其中(“属性值”).lt(数据));
c、 添加(c2);
打破
案例“大于”:
debug(“如果大于,则为属性值”);
Criteria c3=Criteria.where(“attributes.attributeId”).is(值[0])
.andOperator(标准,其中(“属性值”).gt(数据));
c、 添加(c3);
打破
}
query.addCriteria(新条件()和运算符(c.toArray(新条件[c.size()]));
//根据查询条件获取文档
List documents=mongoTemplate.find(查询,documents.class);
第二种方法是使用对象映射器和forEach方法

String[] values = search.split("\\|");
ObjectMapper oMapper = new ObjectMapper();
Object data = dataTypeConvertion(values[0], values[2]);
DocumentsAttributes attributesAnd = new DocumentsAttributes();
attributesAnd.setAttributeId(values[0]);
attributesAnd.setValue(data);
Map<String, Object> att = oMapper.convertValue(attributesAnd, Map.class);
Criteria expression = new Criteria();
att.forEach((key, value) -> expression.and("attributes." + key).is(value));
c.add(expression);


query.addCriteria(new Criteria().andOperator(c.toArray(new Criteria[c.size()])));

return mongoTemplate.find(query, Documents.class);
String[]values=search.split(“\\\\”);
ObjectMapper oMapper=新的ObjectMapper();
对象数据=数据类型转换(值[0],值[2]);
DocumentsAttributes Attributes and=新文档属性();
attributesAnd.setAttributeId(值[0]);
属性和设置值(数据);
Map att=oMapper.convertValue(attributesAnd,Map.class);
条件表达式=新条件();
att.forEach((键,值)->表达式和(“属性”。+key).is(值));
c、 添加(表达式);
query.addCriteria(新条件()和运算符(c.toArray(新条件[c.size()]));
返回mongoTemplate.find(查询、文档、类);

请提前给出解决方案,谢谢。

关于您正在尝试什么,还不是很清楚。如果查询条件是
“attributeId”:“5e2c49a66c1ac06b776b9969”
,那么输出将是
{“attributeId”:“5e2c49a66c1ac06b776b9969”,“value”:NumberLong(67890)}
。感谢您的回复,是的,我正在解析它并将其添加到条件中