Skip to content

Instantly share code, notes, and snippets.

@kaworu
Created March 13, 2014 12:31
Show Gist options
  • Save kaworu/9527549 to your computer and use it in GitHub Desktop.
Save kaworu/9527549 to your computer and use it in GitHub Desktop.
MongoDB shell version: 2.4.8
connecting to: dev
> db.some_collection.ensureIndex({"sub.id":1}, {unique:true});
> db.some_collection.insert({sub:[{id:42}]})
> db.some_collection.find()
{ "_id" : ObjectId("5321a4b172a13ee462bc5c20"), "sub" : [ { "id" : 42 } ] }
> db.some_collection.update({}, {$push: {sub: {id:42}}})
> db.some_collection.find()
{ "_id" : ObjectId("5321a4b172a13ee462bc5c20"), "sub" : [ { "id" : 42 }, { "id" : 42 } ] }
> db.some_collection.insert({sub:[{id:42}]})
E11000 duplicate key error index: dev.some_collection.$sub.id_1 dup key: { : 42.0 }
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment