Skip to content

Instantly share code, notes, and snippets.

@ilguzin
Last active December 16, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilguzin/5366961 to your computer and use it in GitHub Desktop.
Save ilguzin/5366961 to your computer and use it in GitHub Desktop.
As comes from official mongo documentation you cannot create unique index on fields from embedded documents. Here is a workaround for those using $push to append values to array of embedded documents.
Mongo shell command
> db.users.update(
{ community:'Coomunity1',
'users.login': {'$ne': 'Login1'}
},
{ $push:
{ users: {login:'Login1', pwd:'*****'} }
},
{upsert: true})
won't add new user into array of 'users' of a collection item as of condition "'users.login': {'$ne': 'Login1'}" is False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment