title: How to add a json in a nested array of a mongodb document using Spring?
body: <p>Document stored in mongodb:</p>
  
  <pre><code>
  {
  "CNF_SERVICE_ID":"1",
  "SERVICE_CATEGORY":"COMMON_SERVICE",
  "SERVICES":[{
      "SERVICE_NAME":"Authentication Service",
      "VERSIONS":[{
              "VERSION_NAME":"AuthenticationServiceV6_3",
              "VERSION_NUMBER":"2",
              "VERSION_NOTES":"test",
              "RELEASE_DATE":"21-02-2020",
              "OBSOLETE_DATE":"21-02-2020",
              "STATUS":"Y",
              "GROUPS":[{
                  "GROUP_NAME":"TEST GROUP",
                  "CREATED_DATE":"",
                  "NODE_NAMES":[
                      ""
                      ],
                  "CUSTOMERS":[{
                      "CUSTOMER_CONFIG_ID":"4",
                      "ACTIVATION_DATE":"21-02-2020",
                      "DEACTIVATION_DATE":"21-02-2020",
                      "STATUS":"Y"
                  }]
              }]
          }]
      }
      ]
  }
  
  </code></pre>
  
  <p>Now, I need to add another customer json to the array "CUSTOMERS" inside "GROUPS" in the same document above. The customer json would be like this:</p>
  
  <pre><code>{
      "CUSTOMER_CONFIG_ID":"10",
      "ACTIVATION_DATE":"16-03-2020",
      "DEACTIVATION_DATE":"16-03-2021",
      "STATUS":"Y"
  }
  
  </code></pre>
  
  <p>I tried this: </p>
  
  <pre><code>
  Update update = new Update().push("SERVICES.$.VERSIONS.GROUPS.CUSTOMERS",customerdto);
  mongoOperations.update(query, update, Myclass.class, "mycollection");
  
  
  </code></pre>
  
  <p>But, I am getting the exception: org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 28 (PathNotViable): 'Cannot create field 'GROUPS' in element</p>
  
  <p><hr>
  [ EDIT ADD ]</p>
  
  <p>I was able to update it using the filtered positional operator. Below is the query I used:</p>
  
  <pre><code> update( 
     { "SERVICE_CATEGORY":"COMMON_SERVICE", "SERVICES.SERVICE_NAME":"Authentication Service", "SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"}, 
     { $push:{"SERVICES.$[].VERSIONS.$[].GROUPS.$[].CUSTOMERS": { "CUSTOMER_CONFIG_ID":"6", "ACTIVATION_DATE":"31-03-2020", "STATUS":"Y" } } } 
   );
  </code></pre>
  
  <p>Actually, this query updated all the fields irrespective of the filter conditions. So. I tried this but I am facing syntax exception. Please help. </p>
  
  <pre><code>update(
   {"SERVICE_CATEGORY":"COMMON_SERVICE"},
   {"SERVICES.SERVICE_NAME":"Authentication Service"},
   {"SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"}
   {
      $push:{"SERVICES.$[service].VERSIONS.$[version].GROUPS.$[group].CUSTOMERS":{
          "CUSTOMER_CONFIG_ID":"6",
          "ACTIVATION_DATE":"31-03-2020",
          "STATUS":"Y"
      }
      }
   },
   {
          multi: true,
          arrayFilters: [ { $and:[{ "version.VERSION_NAME": "AuthenticationServiceV6_3"},{"service.SERVICE_NAME":"Authentication Service"},{"group.GROUP_NAME":"TEST GROUP"}]} ]
      }
   );
  
  </code></pre>
  
  <p>Update: April 1,2020</p>
  
  <p>The code I tried:</p>
  
  <pre><code>validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));
  Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("s.SERVICE_NAME").is(servicedetail.getService_name()).and("v.VERSION_NAME").is(version.getVersion_name()));
  mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
  
  </code></pre>
  
  <p>The below exception is thrown:</p>
  
  <p>ERROR com.sample.amt.mongoTemplate.MongoOperations - Exception in count(query, collectionName,key,env) :: org.springframework.dao.DataIntegrityViolationException: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'SERVICE_CATEGORY' and 's'; nested exception is com.mongodb.MongoWriteException: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'SERVICE_CATEGORY' and 's'</p>