Reference in Mongo Java driver

206 views
Skip to first unread message

Rina

unread,
Mar 27, 2011, 7:31:09 AM3/27/11
to mongodb-user
I used mongo-java driver in my scala lift prject.
How can I implement reference field?

Scott Hernandez

unread,
Mar 27, 2011, 5:18:37 PM3/27/11
to mongod...@googlegroups.com
Do you mean a DBRef?

http://www.mongodb.org/display/DOCS/Database+References


Or do you mean conceptually like a join table in the relational world?

On Sun, Mar 27, 2011 at 11:31 AM, Rina <rinab...@gmail.com> wrote:
> I used mongo-java driver in my scala lift prject.
> How can I implement reference field?
>

> --
> 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.
>
>

Shulamit Tal

unread,
Mar 28, 2011, 11:13:46 AM3/28/11
to mongodb-user
I want to define in mongo-jave-driver the object
{
"name" : "Joe",
"ColorId" : [
{
"$ref" : "ColorColl",
"$id" : ObjectId("4b0552b0f0da7d1eb6f126a1")
}
],
"_id" : ObjectId("4b0552e4f0da7d1eb6f126a2")
}


I write the code :
val colorId:DBObject=colorColl.findOne(new
BasicDBObject("ColorName","pink"),new BasicDBObject("_id",1))
val myDbRef:DBRef=new DBRef(db,"ColorColl",colorId)
doc.put("ColorId",myDbRef)
shapeColl.insert(doc)
the result for these lines
{ "_id" : { "$oid" : "4d908c2b9b443829948426a3"} , "ColorId" :
{ "$ref" : "ColorColl" , "$id" : { "_id" : { "$oid" :
"4d908c2a9b443829928426a3"}}} , "$id" : { "$ref" : "ColorColl",
"$id" : "{ "_id" : { "$oid" : "4d908c2a9b443829928426a3"}}" }}

so, I try the code
val myDbRef:DBRef=new DBRef(db,"ColorColl",colorId)
val info=new BasicDBObject()
info.put("$ref",myDbRef.getRef)
info.put("$id",myDbRef.getId)
doc.put("ColorId",Array(info))
shapeColl.insert(doc)
the result was long error about the $ref and the $id

On Mar 27, 11:18 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Do you mean a DBRef?
>
> http://www.mongodb.org/display/DOCS/Database+References
>
> Or do you mean conceptually like a join table in the relational world?
>

Scott Hernandez

unread,
Mar 28, 2011, 11:31:15 AM3/28/11
to mongod...@googlegroups.com, Shulamit Tal
Yes, you want to use the DBRef object, not create a DBObject with
$ref/$id manually.

http://api.mongodb.org/java/2.6-pre-/com/mongodb/DBRef.html

Reply all
Reply to author
Forward
0 new messages