Will DBRef be able to work with custom IDs?

88 views
Skip to first unread message

Phillip B Oldham

unread,
Aug 6, 2009, 9:19:43 AM8/6/09
to mongodb-user
Will DBRef be able to work with custom IDs at any point? The docs say
currently it's only mongodb's oid type, but we're porting from another
system which uses UUIDs and need to keep the references intact.

Michael Dirolf

unread,
Aug 6, 2009, 9:58:00 AM8/6/09
to mongod...@googlegroups.com
> Will DBRef be able to work with custom IDs at any point? The docs say
> currently it's only mongodb's oid type, but we're porting from another
> system which uses UUIDs and need to keep the references intact.

The documentation on this seems to be perennially confusing / lacking
- this is something we really need to clean up. New style DBRefs (see http://www.mongodb.org/display/DOCS/DB+Ref+Specification)
do support _id's of any type. In fact these DBRefs are really just
documents with special reserved key names.

Old style DBRefs (as documented on the BSON page) only support
ObjectID - that is why they're deprecated.

To further confuse the issue, what the driver is actually doing when
you create a DBRef instance seems to be different across the drivers
right now - some create new style, some old style.

So to answer your question - use new style DBRefs - they work
everywhere and support _id of any type. We'll work on the docs.

Phillip B Oldham

unread,
Aug 6, 2009, 10:14:21 AM8/6/09
to mongodb-user
> To further confuse the issue, what the driver is actually doing when  
> you create a DBRef instance seems to be different across the drivers  
> right now - some create new style, some old style.

I thought there was something odd going on with this - I'm using both
the console and pymongo. Creating a DBRef from the console results in
a doc with "ns" and "id" keys, whereas pymongo produces "$ref" and
"$id".

> So to answer your question - use new style DBRefs - they work  
> everywhere and support _id of any type. We'll work on the docs.

Can you clear something up for me? Is $ref/$id the new style? If so,
when will the console be updated to catch up? Or is the console the
most up-to-date? If so, there's a bug in that you can't use custom ids
with DBRef which I should post to JIRA.

Eliot Horowitz

unread,
Aug 6, 2009, 10:16:00 AM8/6/09
to mongod...@googlegroups.com
$ns/$id is the new style and needs no special driver support.

So to create a new style dbref in the console, just do something like this:

db.foo.save( { name : "foo" , ref : { $ns : "other" , $id : 123 } } );

Phillip B Oldham

unread,
Aug 6, 2009, 10:19:38 AM8/6/09
to mongodb-user
On Aug 6, 3:16 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> $ns/$id is the new style and needs no special driver support.
>
> So to create a new style dbref in the console, just do something like this:
>
> db.foo.save( { name : "foo" , ref : { $ns : "other" , $id : 123 } } );

So the DBRef() class in the console is deprecated also?

Eliot Horowitz

unread,
Aug 6, 2009, 10:21:13 AM8/6/09
to mongod...@googlegroups.com
yes - the type itself is deprecated. in the console DBRef == type DBRef

we need a new name for the new style, or rename the old style to avoid
confusion.

there is no special type for the new style, its just a standard.

Phillip B Oldham

unread,
Aug 6, 2009, 10:29:13 AM8/6/09
to mongodb-user
On Aug 6, 3:21 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> yes - the type itself is deprecated.  in the console DBRef == type DBRef
>
> we need a new name for the new style, or rename the old style to avoid
> confusion.
>
> there is no special type for the new style, its just a standard.

> db.test.find()
{"_id" : "b44ab2b6-c502-11dd-b0b6-001d7dc91168"}
> db.main.save({ "_id" : "b45c046c-c502-11dd-b0b6-001d7dc91168", "_ref": {"$ns": "test", "$id": "b44ab2b6-c502-11dd-b0b6-001d7dc91168"}})
> db.main.findOne()
{"_id" : "b45c046c-c502-11dd-b0b6-001d7dc91168" , "_ref" : {"$ns" :
"test" , "$id" : "b44ab2b6-c502-11dd-b0b6-001d7dc91168"}}
> db.main.findOne()._ref.fetch()
Thu Aug 6 15:24:03 JS Error: TypeError: db.main.findOne()._ref.fetch
is not a function (shell):0
> db.main.save({ "_id" : "b45c046c-c502-11dd-b0b6-001d7dc91168", "_ref": {"ns": "test", "id": "b44ab2b6-c502-11dd-b0b6-001d7dc91168"}})
> db.main.findOne()._ref.fetch()
Thu Aug 6 15:24:17 JS Error: TypeError: db.main.findOne()._ref.fetch
is not a function (shell):0
> db.main.save({ "_id" : "b45c046c-c502-11dd-b0b6-001d7dc91168", "_ref": {"$ref": "test", "$id": "b44ab2b6-c502-11dd-b0b6-001d7dc91168"}})
> db.main.findOne()._ref.fetch()
Thu Aug 6 15:25:35 JS Error: TypeError: db.main.findOne()._ref.fetch
is not a function (shell):0

So I'm guessing .fetch() is deprecated also?

Eliot Horowitz

unread,
Aug 6, 2009, 10:35:34 AM8/6/09
to mongod...@googlegroups.com
fetch() is only part of the DBRef class.
If we make a NewDBRef class, we can add fetch() back.
But before we go too far, I think we need to get the names cleaned up.

And it seems i made a mistake in what i said, its { $ref : <namespace>
, $id : 123 } (not $ns) sorry about that.

Phillip B Oldham

unread,
Aug 6, 2009, 10:42:22 AM8/6/09
to mongodb-user
> fetch() is only part of the DBRef class.
> If we make a NewDBRef class, we can add fetch() back.

If it *does* get added back, there's a JIRA ticket/feature request for
a complimentary fetchAll() method. Not sure if you fellows have seen
that yet.

> But before we go too far, I think we need to get the names cleaned up.

Agreed/understood.

> And it seems i made a mistake in what i said, its { $ref : <namespace>
> , $id : 123 } (not $ns)  sorry about that.

No worries. I'll stop bugging the list about this issue now and wait
for the docs. :)
Reply all
Reply to author
Forward
0 new messages