[JAVA-387] DBCollection#findOne(DBObject o, DBObject fields) doesn't markAsPartialObject Created: 24/Jun/11  Updated: 10/Aug/11  Resolved: 19/Jul/11

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.6.3
Fix Version/s: 2.6.4

Type: Bug Priority: Major - P3
Reporter: Marc Leidner Assignee: Antoine Girbal
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

/Applications/mongodb-osx-x86_64-1.8.1/bin/mongod
/lib/mongo-2.6.3.jar



 Description   

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();



 Comments   
Comment by Antoine Girbal [ 19/Jul/11 ]

thanks for report.
The check was only done in DBCursor, which findOne doesnt use.

Comment by auto [ 19/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: - JAVA-387: DBCollection#findOne(DBObject o, DBObject fields) doesn't markAsPartialObject
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/3aee72f1cf814bcbc786a2e04dbce12a81f4cc14

Generated at Thu Feb 08 08:52:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.