I’m facing a scenario where I need to rename a field across a large MongoDB collection consisting of approximately 1 million documents. I’m looking for the most efficient way to accomplish this task to minimize downtime and ensure optimal performance.
Here’s the current approach I’m using with Mongoose:
async function renameField() {
await mongoose.connect(MONGODB_URL);
const userModel = mongoose.connection.db.collection("users");
const result = await userModel.updateMany({}, { $rename: { new_email: "email" } });
}
renameField().catch(console.error);