DBRef in Java

1,883 views
Skip to first unread message

Martinus Martinus

unread,
Nov 15, 2011, 12:20:20 PM11/15/11
to mongod...@googlegroups.com
Hi,

I'm wanted to know how to used DBRef in Java driver, so basically I have this kind of documents about 100 of them with same "mchid", but different timestamp :

collection place
{
     "_id" : ObjectId("4b0552e4f0da7d1eb6f126a2"),
     "users" : 10,
     "male" : 5,
     "female" : 5,
     "mchid" : ObjectId("4b0552e4f0da7d1eb6f126a3"),
       "pos" : [50, 40],
       "time" : 1312121212
}

and I want to refer to this document from another collection in the same database, like below :

collection machine
{
     "_id" : ObjectId("4b0552e4f0da7d1eb6f126a5"),
     "machine" : "server",
     "machine_id" : [{"$ref" : "place", "$id" : "mchid"}]
}

Would you be so kindly to give an example from this simple case?

Thanks.

Marc

unread,
Nov 15, 2011, 2:23:11 PM11/15/11
to mongodb-user
From the documentation:
"Java supports DB references using the DBRef class."
http://www.mongodb.org/display/DOCS/Database+References

You can make a DataBase reference in java like so:

import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBRef;
import org.bson.types.ObjectId;
import com.mongodb.DBObject;

Mongo connection = new Mongo("localhost", 27017);
DB db = connection.getDB("test");
DBRef myDbRef = new DBRef(db, "place", new
ObjectId("4b0552e4f0da7d1eb6f126a2"));
DBObject doc = myDbRef.fetch();
System.out.println(doc);

The above prints the referenced document:

{ "_id" : { "$oid" : "4b0552e4f0da7d1eb6f126a2"} , "users" : 10.0 ,
"male" : 5.0 , "female" : 5.0 , "mchid" : { "$oid" :
"4b0552e4f0da7d1eb6f126a3"} , "pos" : [ 50.0 , 40.0] , "time" :
1.312121212E9}

For further reading, here is a link to the documentation on the DBRef
class:
http://api.mongodb.org/java/current/com/mongodb/DBRef.html

Hopefully the above will help you accomplish what you need to do.
Good Luck!

Martinus Martinus

unread,
Nov 15, 2011, 9:30:09 PM11/15/11
to mongod...@googlegroups.com
Hi Marc,

Thanks for your example. So, If I do this : DBRef myDbRef = new DBRef(db, "place", new ObjectId("4b0552e4f0da7d1eb6f126a3"));, it will gave me the same result right?

Thanks.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Marc

unread,
Nov 16, 2011, 11:32:07 AM11/16/11
to mongodb-user
I want to say, "yes", but I am not quite sure what you mean by, "the
same result". The line:
DBRef myDbRef = new DBRef(db, "place", new
ObjectId("4b0552e4f0da7d1eb6f126a3"));
will create a DBRef object in Java, but it is only a reference. If
you want to return the document that the DBRef object is referencing,
you must use the .fetch() command as I did in my example.
Reply all
Reply to author
Forward
0 new messages