We have around 20 million records in our mongodb. In my collection called ‘posts’ there is a field called ‘id’ which was supposed to be unique but now it has gotten all messed up. We just want it to be unique and there are many many duplicates now.
We just wanted to do something like iterating over every reocrd and assigning it a unique id in a loop from 1 to 20million.
What would be the easiest way to do this?
You can use a simple script like this:
save it to assignids.js, and run as
the outer-while selects 1000 rows as a time, and prevents cursor timeouts; the inner while assigns each row a new incremental id.