title: Delete reference of one object from another object using Spring Data Mongo
body: <p>My BOLReference object which is the main object looks like below:</p>
  <pre><code>{
    &quot;_id&quot; : {
      &quot;bolNumber&quot; : &quot;ISH208117&quot;,
      &quot;complianceId&quot; : &quot;DKIMP-M1A1PBZ5SWE91OZ3GYP6QA&quot;
    },
    &quot;isIVCRecieved&quot; : true,
    &quot;isMandatoryVehicleDetailsCheckRequired&quot; : false,
    &quot;workflowExceptions&quot; : [{
        &quot;category&quot; : &quot;Missing Reference&quot;,
        &quot;businessKeyValues&quot; : {
          &quot;ISH208117_7KYRwcSuZM&quot; : &quot;Missing Reference&quot;
        }
      }],
  }
  </code></pre>
  <p>And my child object called WorkflowException whose reference should be deleted from BOLReference object looks like:</p>
  <pre><code>{
    &quot;_id&quot; : &quot;ISH208117_7KYRwcSuZM&quot;,  // this is the business key
    &quot;exceptionType&quot; : &quot;Missing reference&quot;,  // this is the category
    &quot;exceptions&quot; : [&quot;Missing Reference Exception Occurred&quot;],
    &quot;billOfLadingId&quot; : &quot;ISH208117&quot;,
    &quot;estimatedTimestamp&quot; : &quot;2021-09-20T04:00:00.000&quot;,
    &quot;vesselCode&quot; : &quot;1QM&quot;,
  
  }
  </code></pre>
  <p>I'm trying below code but unable to delete the reference of WorkflowException from BOLRef object.</p>
  <pre><code>    @Autowired
      @Qualifier(&quot;complianceMongoTemplate&quot;)
      private MongoTemplate complianceMongoTemplate;
  
      public String cleanseWorkFlow(String businessKey) {
          
          Query find = new Query();
          find.addCriteria(Criteria.where(&quot;_id.bolNumber&quot;).is(businessKey.split(&quot;_&quot;)[0]));
          BOLReference bolRef = complianceMongoTemplate.findOne(find, BOLReference.class);
          Optional&lt;WorkflowExceptions&gt; wf = bolRef.getWorkflowExceptions().stream()
                  .filter(bk -&gt; bk.getBusinessKeyValues().containsKey(businessKey)).findFirst();
  
          if(wf.isPresent()) {
          Query removeQuery = new Query();
          removeQuery.addCriteria(Criteria.where(&quot;workflowExceptions&quot;).is(wf.get()).and(&quot;_id.bolNumber&quot;)
                  .is(bolRef.getId().getBolNumber()));
  
          List&lt;BOLReference&gt; wc = complianceMongoTemplate.findAllAndRemove(removeQuery, BOLReference.class);
          log.info(&quot;removed business keys from bol reference {} {}&quot;, businessKey, wc.size());
          }
          
          return businessKey;
      }
  </code></pre>