mongoDB: unique index on a repeated value -


so i'm pretty new mongodb figure misunderstanding on general usage. bear me. have document schema i'm working such

  {      name: "bob",      email: "bob@gmail.com",      logins: [        { u: 'a', p: 'b', public_id: '123' },        { u: 'x', p: 'y', public_id: 'abc' }     ]    } 

my problem need ensure public ids unique within document , collection, furthermore there existing records being migrated mysql db dont have records, , therefore replaced null values in mongo.

i figure either index

db.users.ensureindex({logins.public_id: 1}, {unique: true}); 

which isn't working because of missing keys , throwing e11000 duplicate key error index:

or more fundamental schema problem in shouldn't nesting objects in array structure that. in case, what? seperate collection user_logins??? seems go against idea of embedded document.

if expect u , p have same values on each insert (as in example snippet), might want use $addtoset operator on inserts ensure uniqueness of public_id field. otherwise think it's quite difficult make them unique across whole collection not working external maintenance or js functions.

if not, possibly store them in own collection , use public_id _id field ensure cross-document uniqueness inside collection. maybe contradict idea of embedded docs in doc database, according different requirements think that's negligible.


Comments

Popular posts from this blog

how to build hyperlink for query string in php -

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

queue - mq_receive: message too long -