mongodb - Randomizing unique data with mongo? -


i'm trying scramble email properties in db. email defined in mongoose model unique. here's script i'm attempting run in shell

db.getcollection('users').update(   {},   {$set{     email:'sanitized'+math.random()*100000000000000000+'@'+math.random()*100000000000000000+'.com'   }},   {multi:true} ) 

i'm trying this:

but comes error:

duplicate key error index: test.users.$email_1 dup key

i realize math.random() isn't perfect, command has never updated more first document in collection.

how can want do?

the math.random functions executing once each client-side , 1 constant email value being passed update function.

if there not unique index on email, every user in database set same email.

you individual updates this:

db.getcollection('users').find().foreach(function(u){   db.users.update({_id : u._id}, {$set:{     email:'sanitized'+math.random()*100000000000000000+'@'+math.random()*100000000000000000+'.com'   }}) }) 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -