Unique indexes on embedded documents

1,080 views
Skip to first unread message

rikkicarroll

unread,
Jun 15, 2010, 9:24:30 AM6/15/10
to mongodb-user
Hello,

I have the following collection:
{
name: 'Account name 1;,
users: [
{
email: 'em...@email.com',
password: 'abc'
},
{
email: 'em...@email.com',
password: 'abc'
}
]
}

db.accounts.ensureIndex({'users.email': 1}, {unique:true});

However when I add the index it will allow two 'embedded' users in the
same document to have the same email address, but not two users across
documents. Is there a way to get the unique indexes to ensure that no
two users have the same email across embedded documents and top level
documents.

Thanks,
Rikki

Kyle Banker

unread,
Jun 15, 2010, 9:30:44 AM6/15/10
to mongod...@googlegroups.com
There's an issue related to this:

How are you adding to the collection? If you use $push, you might try something like

db.accounts.update({_id: id, 'users.email': {'$ne': some_email}}, {'$push': {users: {email: some_email, password: 'abc'}}})

This will succeed only if the email doesn't yet exists in the users array.


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


rikkicarroll

unread,
Jun 15, 2010, 9:50:16 AM6/15/10
to mongodb-user
Thanks for the super quick reply - awesome and super neat work around!

There are two ways we are adding users to the document.

The first is as you suggested using the $push operator after the
document is created (but without the nice $ne workaround in the
criteria).

The second is when the account is originally created, if multiple
users are specified it will build the document and then insert the
whole thing with multiple users at once. I guess the work around for
that is to insert the account and then do the users with separate
queries.

I started to re-factor it so users is a separate collection, but that
has its own issues so would like to do the single document with
embedded documents for the users.

Thanks for your help

Rikki
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .

Nico Taing

unread,
Feb 16, 2012, 5:25:55 PM2/16/12
to mongod...@googlegroups.com, nat...@bazaarlabs.com
Hi Kyle,

I'm having some issues to create unique indexes on embedded documents.
Is that still true? `db.accounts.ensureIndex({'users.email': 1}, {unique:true});` doesn't work?

In an ideal world, I would like mongodb reject by itself if I try to add a duplicate embedded doc like on a root document.

Scott Hernandez

unread,
Feb 16, 2012, 5:36:24 PM2/16/12
to mongod...@googlegroups.com
This issue is still open, yes. https://jira.mongodb.org/browse/SERVER-1068

Unique just applied to the documents in the collection, not the
embedded elements in the document. You would only want to add an
embedded doc if it didn't already have that email, to keep the value
unique within the document. You can do this in the update query +
operations.

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/U7OOWF_rvCsJ.

Nico Taing

unread,
Feb 16, 2012, 5:39:39 PM2/16/12
to mongod...@googlegroups.com
Ok, thanks for your quick response Scott.
Reply all
Reply to author
Forward
0 new messages