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

DBCollection#findOne(DBObject o, DBObject fields) doesn't markAsPartialObject

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.6.4
    • Affects Version/s: 2.6.3
    • Component/s: API
    • None
    • Environment:
      /Applications/mongodb-osx-x86_64-1.8.1/bin/mongod
      /lib/mongo-2.6.3.jar

      Looks like a bug in the driver where markAsPartialObject is not set when querying with findOne(DBObject o, DBObject fields).
      In effect a collection.save(object) causes loss of unselected fields.

      This code illustrates the behavior (at least on my machine):

      DBCollection col = new Mongo("localhost").getDB("test").getCollection("test");
      col.drop();

      // insert document with two fields:
      DBObject t = BasicDBObjectBuilder.start("name", "marc").add("company", "viadesk").get();
      col.save(t);

      // prepare query and field selector:
      DBObject q = BasicDBObjectBuilder.start("name", "marc").get();
      DBObject f = BasicDBObjectBuilder.start("name", 1).get();

      // call findOne to get object, returns full document:
      DBObject r = col.findOne(q);
      System.out.println("findOne (all fields):");
      System.out.println(r);
      System.out.println("isPartial: " + r.isPartialObject());
      System.out.println();

      // call findOne to get object, now with only 'name' field,
      // notice isPartialObject returns false!
      r = col.findOne(q, f);
      System.out.println("findOne (selected fields):");
      System.out.println(r);
      System.out.println("isPartial: " + r.isPartialObject());
      System.out.println();

      // try to save the returned document, this should throw an
      // IllegalArgumentException: can't save partial objects
      // but is doesn't:
      col.save(r);
      System.out.println("saved document");
      System.out.println();

      // call findOne to get object, now the 'company' field gone
      r = col.findOne(q);
      System.out.println("findOne (all fields):");
      System.out.println(r);
      System.out.println("isPartial: " + r.isPartialObject());
      System.out.println();

            Assignee:
            antoine Antoine Girbal
            Reporter:
            leidner Marc Leidner
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: