Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-502

cannot remove nulls from array

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.7.2
    • Component/s: API, Documentation
    • Environment:
      Java Version: 1.6.0_23 Sun Microsystems Inc. -- OpenJDK Server VM
      Server: jetty
      Hardware: i386
    • Fully Compatible

      I use the following update:

      collection.update(q, new BasicDBObject("$pull", new BasicDBObject("x",null))
      //where q is some object which queries the correct doc in the collection and "x" corresponds to an array

      and I receive the following error:

      2012.01.05 10:34:36 root - Error: java.lang.NullPointerException: can't save a null object
      java.lang.NullPointerException: can't save a null object
      at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:95)
      at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
      at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
      at com.mongodb.OutMessage.putObject(OutMessage.java:142)
      at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:345)
      at com.mongodb.DBCollection.update(DBCollection.java:165)
      at com.mongodb.DBCollection.update(DBCollection.java:197)
      at com.mongodb.DBCollection.update(DBCollection.java:209)

      The null is created in the first place as a result of using the $unset:

      collection.update(q, new BasicDBObject("$unset", new BasicDBObject("x"+".$",1)))
      //where q is some object which queries the correct doc in the collection and "x" corresponds to an array

      I cannot use the $pull in replacement of $unset because i would require exact matching, which doesn't seem possible as documented:
      "Because of this feature, to use the embedded doc as a match criteria, you cannot do exact matches on array elements."

      I had assumed that using the null pointer reference was ok based on the post by Andrew Wharton specified in: http://www.mongodb.org/display/DOCS/Java+Types

      Is this the correct way to be doing this? The only other way to accomplish this would be to query the entire array, modify it, then insert it back; but I would rather not do this as this could lead to a race condition.

            Assignee:
            scotthernandez Scott Hernandez (Inactive)
            Reporter:
            jrogers Josh Rogers
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: