using DBRef in Casbah

465 views
Skip to first unread message

devnewb

unread,
Feb 6, 2011, 4:45:58 PM2/6/11
to mongodb-casbah-users
Hi guys.

I am looking for help using DBRef, can anyone post a quick sample from
a REPL session?

Thanks for your help.

devnewb

unread,
Feb 6, 2011, 5:03:27 PM2/6/11
to mongodb-casbah-users
Perhaps there is an alternative way ? I am just unfamiliar with this,
new to it.

Brendan W. McAdams

unread,
Feb 6, 2011, 5:05:55 PM2/6/11
to mongodb-ca...@googlegroups.com

Casbah doesn't provide any special wrapper to DBref. You'll need to instantiate a new instance of com.mongodb.DBRef. 

On Feb 6, 2011 5:03 PM, "devnewb" <cmsmy...@gmail.com> wrote:

devnewb

unread,
Feb 6, 2011, 6:25:03 PM2/6/11
to mongodb-casbah-users
Hi Brendan.

Is there a simpler more idiomatic alternative ? I have something
working with DBRef but its kinda painful :-)

Thanks.

On Feb 6, 10:05 pm, "Brendan W. McAdams" <bren...@10gen.com> wrote:
> Casbah doesn't provide any special wrapper to DBref. You'll need to
> instantiate a new instance of com.mongodb.DBRef.
> On Feb 6, 2011 5:03 PM, "devnewb" <cmsmyth...@gmail.com> wrote:

devnewb

unread,
Feb 6, 2011, 6:26:50 PM2/6/11
to mongodb-casbah-users
Hi Brendan.

Is there a simpler more idiomatic alternative ? I have something
working with DBRef but it is kinda painful :-)

Thanks.

On Feb 6, 10:05 pm, "Brendan W. McAdams" <bren...@10gen.com> wrote:
> Casbah doesn't provide any special wrapper to DBref. You'll need to
> instantiate a new instance of com.mongodb.DBRef.
> On Feb 6, 2011 5:03 PM, "devnewb" <cmsmyth...@gmail.com> wrote:

Brendan W. McAdams

unread,
Feb 7, 2011, 12:03:39 PM2/7/11
to mongodb-ca...@googlegroups.com
There currently isn't, mostly because in almost all cases the usage of DBRefs are an antipattern.


As such, I've taken care not to add any Scala idioms for DBRef to Casbah which would encourage uneducated use of them.  Those who want them must consider them carefully and seek out the behavior.

Two things to consider:

a) It is almost always preferred to embed documents than reference them.  For each DBRef in a document you will need to make another call to MongoDB, there is no server-side joining behavior.  

b) In many cases, the use of DBRefs even in valid referencing behavior is unnecessary.  All that DBRef does is point to a particular namespace (DB + Collection) and ID.  There is no foreign key relationship or cascading: One can easily create a DBRef to a non-existent document, db or collection.  If someone deletes the referenced document it does not remove the ref.  It is usually best to simply embed the ID of the document you want to reference as long as you know what collection it is in.  We recommend only using DBRef if the containing collection is unknown and will need to be resolved.

Should you want to pursue DBRef further, you'll need to look at the JavaDocs: http://api.mongodb.org/java/2.5-pre-/com/mongodb/DBRef.html

DBRef is imported in Casbah so it should be available.
Reply all
Reply to author
Forward
0 new messages