mongodb - How do I ensure uniqueness in an array of subdocuments in Mongo -


i have collection of documents following structure

{ "_id" : objectid("531db35c4e08d754e036d4c1"), "name" : "group 1", "description" : "first group", "recipients" : [     {         "cellnumber" : "27123456789",     },     {         "cellnumber" : "27987654321",     },     {         "cellnumber" : "27837939043",     } ], "creationdate" : numberlong("1394455388960"), "accountkey" : "b6d45025bfdc5d2d1e5fa158a2729fcc" } 

i have requirement each recipient in group document uniquely identified cellnumber. however, multiple groups can have same recipient. how ensure each group has recipient subdocument each recipient unique. here tried way of ensureindex

> db.groups.ensureindex({"name":1, "recipients.cellnumber":1}, {unique:true}) 

that did not seem work additions of recipients existing numbers went through without errors. possible uniquely index subdocument array?

thanks.

there $addtoset operator can use when updating elements in array (set) may worth looking into. in case use this:

db.groups.update({     $addtoset: { "receipients": { "cellnumber": "27123456789" } } }) 

and make sure values in recipients field, long all keyed cellnumber unique.

outside of managing in code.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -