MongoDB: Pull using Java Driver
<p>Here I am learning MongoDB. And I am stuck at performing a very basic operation using the MongoDB Java driver. I am trying to remove an element from an array present in the document. The document looks like this:</p>

<pre><code>db.test.find().pretty()
{
    "_id" : ObjectId("581245dd51030d389f5cf701"),
    "name" : "Rahul",
    "scores" : [
        {
            "SDM" : 97
        },
        {
            "SE" : 96
        },
        {
            "DM" : 80
        }
    ]
}
</code></pre>

<p>I am trying to remove the second element in the array i.e. <code>{"SE" : 96}</code>.  I understand that I can achieve this using <code>coll.update(match, new BasicDBObject("$pull", update));</code>. However I would like to leverage the <code>com.mongodb.client.model.Updates.pull(final String fieldName, final TItem value)</code>method.</p>

<p>This is what I tried before posting the question:</p>

<p>1) I expected nothing to happen when I tried this and indeed there was no modification to the document.</p>

<pre><code>  ` Bson filter = Filters.eq("name", "Rahul");
    Bson delete = Updates.pull("SE", 96);
    collection.updateOne(filter, delete);`
</code></pre>

<p>2) The following is throwing an exception.</p>

<pre><code>  ` Bson filter = Filters.eq("name", "Rahul");
    Bson delete = Updates.pull("scores.SE", 96);
    collection.updateOne(filter, delete);`
</code></pre>

<p>Exception: </p>

<pre><code>Exception in thread "main" com.mongodb.MongoWriteException: cannot use the part (scores of scores.SE) to traverse the element ({scores: [ { SDM: 97.0 }, { SE: 96.0 }, { DM: 80.0 } ]})
    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:523)
    at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:508)
    at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:355)
    at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:350)
    at com.mongodb.mongoProject.crud.UpdateTest.main(HW_3_1.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
</code></pre>

<p>Please let me know where am I going wrong with this.</p>

<p>Thanks...</p>