Various Doc Types on Unique Compound Keys

5 views
Skip to first unread message

Jim Zhan

unread,
Oct 14, 2009, 7:50:17 AM10/14/09
to mongodb-user
I have various docs on a collection with various unique compound keys
like this:

collections: resources

{"name" : "serial_-1_type_-1_owner_-1" , "ns" : "mydb.resources" ,
"key" : {"serial" : -1 , "type" : -1 , "owner" : -1} , "unique" :
true}
{"name" : "url_-1_type_-1_owner_-1" , "ns" : "mydb.resources" ,
"key" : {"url" : -1 , "type" : -1 , "owner" : -1} , "unique" : true}

some records have serial field while some have url only (type & owner
are common),
I then got duplicated error when saving any kinda new record, any
advice?

Tks
Jim

Eliot Horowitz

unread,
Oct 14, 2009, 9:26:47 AM10/14/09
to mongod...@googlegroups.com
Well given those indexes, serial,type,owner and url,type,owner have to
be unique.
Do those have to be unique indexes?

Jim Zhan

unread,
Oct 14, 2009, 10:04:01 AM10/14/09
to mongodb-user
Sorry that I don't quite understand, are you saying that if I have
compound key index like this: {"name" : "serial_-1_type_-1_owner_-1" ,
"ns" : "mydb.resources" ,
"key" : {"serial" : -1 , "type" : -1 , "owner" : -1} , "unique" :
true}, then I have to make sure the record has "serial", "type" and
"owner",
otherwise, "type" & "owner" will be unique?

Eliot Horowitz

unread,
Oct 14, 2009, 3:40:53 PM10/14/09
to mongod...@googlegroups.com
Well you have a unique compound index.
When you created it you must have made it unique.
But if you don't make it unique - obviously that constraint goes away

Mathias Stearn

unread,
Oct 14, 2009, 4:11:16 PM10/14/09
to mongod...@googlegroups.com
If I'm understanding your schema you have a collection objects that are identified by *either* a url or a serial. In that case try something like this:

{_id:"identifying value", id_is:"serial or url", type:"", owner:""}

--Mathias

Jim Zhan

unread,
Oct 14, 2009, 9:13:43 PM10/14/09
to mongodb-user
its kinda like this but more complicated, as I have various resources
like bookmarks, files, contacts etc, they are identified by various
unique compound keys. if I'm understanding the approach Mongo is
using correctly, we should only pick the most common fields in various
docs and make them unique, as a record does not have the field which
meet the unique compound key constrain, say the key is
"serial_-1_type_-1_owner_-1",
and a bookmark record does not have "serial", Mongo recognise it as
"serial=null",
so I will get duplicated error if I insert another bookmark record,
correct?

Eliot Horowitz

unread,
Oct 14, 2009, 9:45:53 PM10/14/09
to mongod...@googlegroups.com
if this is a key and it has to be unique
{ serial : null , type : x , owner : x }
then you can only have entry like that in the collection.

Jim Zhan

unread,
Oct 14, 2009, 11:07:13 PM10/14/09
to mongodb-user
understood, then seems there's no any other way to achieve it.
and we don't have any kinda cross-collection search support in 1.0,
right?
Reply all
Reply to author
Forward
0 new messages