title: Remove an array entry in an object using spring data mongodb
body: <p>I recently spent some time trying to use the <code>$pull</code> operator through Spring's Data MongoOperations interface, so I thought it would be nice to share my findings in case anyone bumps into a similar problem.</p>
  
  <p>So here it goes...</p>
  
  <p>I have 2 java POJOs like so :</p>
  
  <pre><code>@Document
  public class OutterObject{
  
      private String id;
      private String name;
      private List&lt;InnerDocument&gt; innerDocs;
  
  
      //SETTERS - GETTERS Ommited
  
  
  public class InnerDocument{
  
  
      private String id;
      private String name;
  
           //SETTERS - GETTERS Ommited
  </code></pre>
  
  <p>This is stored in a Mongo collection like so :</p>
  
  <pre><code> "_id" : "doc2",
   "_class" : "OutterObject",
   "name" : "doc2",
   "innerDocs" : [{
        "_id" : "innerDoc21",
        "name" : "innerDoc21"
    }, {
        "_id" : "innerDoc22",
        "name" : "innerDoc22"
    }]
  </code></pre>
  
  <p>I'm trying to use the $pull operator in order to remove all objects inside the innerDoc collection having a name value = "innerDoc22".</p>
  
  <p>I know how to accomplish this using the mongo driver like so :</p>
  
  <pre><code> List&lt;String&gt; ids = 
           Arrays.asList("innerDoc22");
  
   BasicDBObject find = new BasicDBObject();
  
   match.put("innerDocs.name", 
             BasicDBObjectBuilder.start("$in", ids).get());
  
      BasicDBObject update = new BasicDBObject();
          update.put(
                  "$pull",
                  BasicDBObjectBuilder.start("innerDocs",
                          BasicDBObjectBuilder.start("name", "innerDoc22").get()).get());
  
    DBCollection col= mongoOperations.getDb().getCollection("outterObject");
  
    col.update(find , update);
  </code></pre>
  
  <p>I'm trying to accomplish the same thing using Spring's MongoOperations Interface.
  Here is my code using the MongoOperations interface :</p>
  
  <pre><code>List&lt;String&gt; ids = Arrays.asList("innerDoc22");
  
  Query removeQuery = Query.query(Criteria.where("innerDocs.name").in(ids));
  
  WriteResult wc = mongoOperations.upsert(
                      removeQuery, 
                      new Update().pull("innerDocs.name", "innerDoc22"), 
                      OutterObject.class);
  System.out.println(wc.getLastError());
  </code></pre>
  
  <p>I'm not getting any errors when calling <code>getLastError()</code> the update is simply not done in the database.</p>
  
  <p>I know a similar question has already been asked <a href="https://stackoverflow.com/questions/10097621/remove-an-entry-from-array-using-mongodb-java-driver">here</a> but the answer that was given does not use the MongoOperations interface.</p>