I have a huge collection (68017 documents) named "inserogato", imported from PostgreSQL. In PostgreSQL the primary key for the table "inserogato" was "id", but MongoDB create a default primary key named "_id" with an ObjectId type. So I want to copy all values in the field "id" to the field "_id".
I've tried this but it only update a document:
db.inserogato.find({"_id" : ObjectId("5abe1d264887072726b19b2e")}).forEach(function(doc) {
var oldId = doc._id;
doc._id = NumberLong(doc.id);
db.inserogato.remove({ _id: oldId });
db.inserogato.save(doc);
});