title: how to get mongoDB object by mongoTemplate query in java, by nested array of object key value
body: <p>Single Document has an array of attributes with attribute-Id and value. 
  I want to retrieve the object by matching the attribute-Id and the values corresponding to each attribute object in the array with multiple operators. 
  ISSUE: While performing the Search GET API, any attribute id which has the value being searched, gets picked up than the particular attribute id which should be the only picked up response.
  Cross-referenced attribute ids being picked up due to them having the same attribute value, is the root cause of the issue.</p>
  
  <p>Below is my MongoDB JSON object </p>
  
  <pre><code>  {
          "_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`
  </code></pre>
  
  <p>tried multiple types of methods.</p>
  
  <pre><code>List&lt;Documents&gt; resultDocument = new ArrayList&lt;Documents&gt;();
  
          // to save list of criteria
          ArrayList&lt;Criteria&gt; c = new ArrayList&lt;Criteria&gt;();
          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&lt;Documents&gt; documents = mongoTemplate.find(query, Documents.class);
  </code></pre>
  
  <p>second method followed by using object mapper and forEach method.</p>
  
  <pre><code>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&lt;String, Object&gt; att = oMapper.convertValue(attributesAnd, Map.class);
  Criteria expression = new Criteria();
  att.forEach((key, value) -&gt; 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);
  </code></pre>
  
  <p>please give solution, thanks in advance.</p>