[JAVA-722] Errors when DBRef's $id is Object instead of simple value Created: 06/Jan/13  Updated: 19/Mar/13  Resolved: 24/Jan/13

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.10.1
Fix Version/s: 2.11.0

Type: Bug Priority: Major - P3
Reporter: Oleksii Lomako Assignee: Trisha Gee
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

any mongod version



 Description   

Mongo is able to store objects in _id field, for example:
db.test.save({_id: {name: "Alex", email: "test@example.com"}});

when i try to make a DBRef from another collection to this record it also works:
db.test2.save({_id: "test2", ref: new DBRef("test",

{ "name" : "Alex", "email" : "test@example.com" }

)});
db.test2.findOne({_id: "test2"}).ref.fetch();

Same stuff in Java just doesn't work:
DBCollection test = db.getCollection("test");
DBCollection test2 = db.getCollection("test2");
test.save(new BasicDBObject("_id", new BasicDBObject("name", "Alex").append("email", "test@example.com")));
test2.save(new BasicDBObject("_id", "test2").append("ref", new DBRef(db, "test", new BasicDBObject("name", "Alex").append("email", "test@example.com"))));
DBObject testOne = test2.findOne(new BasicDBObject("_id", "test2"));
System.out.println(testOne);

...and we got this:
{
"_id" : "test2" ,
"ref" : {
"$ref" : "test" ,
"$id" :

{ "name" : "Alex" , "email" : "test@example.com" }

} ,
"$id" : {
"$ref" : "test" ,
"$id" :

{ "name" : "Alex" , "email" : "test@example.com" }

}
}

as you can see - we've got additional field "$id" that is not in DB!!!
when we try to use DBRef's in collection - we'll get exception in java side.



 Comments   
Comment by auto [ 24/Jan/13 ]

Author:

{u'date': u'2013-01-24T18:01:57Z', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: Merge pull request #96 from mutyonok/master

JAVA-722: Errors when DBRef's $id is Object instead of simple value
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/85fad93165ff2ed72fac25208ab78197520acb5e

Comment by auto [ 24/Jan/13 ]

Author:

{u'date': u'2013-01-07T18:50:13Z', u'email': u'alexey.lomako@gmail.com', u'name': u'mutyonok'}

Message: Fixed JAVA-722 bug, added test cases
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/ed480ceede5f82e8f4ed635a5a8a3251fd8716a3

Comment by Jeffrey Yemin [ 22/Jan/13 ]

Just waiting for a response to comments on the pull request: https://github.com/mongodb/mongo-java-driver/pull/96

Comment by Ben McCann [ 10/Jan/13 ]

Thanks for finding this and creating a pull request. I've been bitten by this myself.

Comment by Jeffrey Yemin [ 10/Jan/13 ]

I'm working on a few other things now, but don't worry, it will get in to
the next release.

Comment by Oleksii Lomako [ 10/Jan/13 ]

Hi Jeff,

Did you have time to merge my pull request?
It is now green in travis and java 5 friendly.

Comment by Jeffrey Yemin [ 06/Jan/13 ]

A pull request would be much appreciated. Make sure you follow the guidelines in https://github.com/mongodb/mongo-java-driver/blob/master/CONTRIBUTING.md. Thanks.

Comment by Oleksii Lomako [ 06/Jan/13 ]

I have a fix for this problem - so I can make a pull request on github and hope you can merge it and release it with the next verion of Mongo java driver as my current work really needs this fix

Ofcourse I know that it is possible to workaround this issue by setting my own DBCallback, but I prefer to programm without any workarounds, especially when you can easilly fix this bug.

Thanks in advance!

Comment by Oleksii Lomako [ 06/Jan/13 ]

The problem is in DefaultDBCallback.java - this callback cannot handle compound Id in DBRef. It doesn't track names of objects when decodes them.

It is very easy to fix the bug - DefaultDBCallback shoud have its own stack of decoded object names (like BasicBSONDecoder) instead of just tracking last decoded name in _lastName variable.

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