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",
)});
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" :
} ,
"$id" : {
"$ref" : "test" ,
"$id" :
}
}
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.