// Assuming you have a collection named "yourCollectionName" and a field named "yourFieldName" // Connect to your MongoDB database use yourDatabaseName; // Update the documents in the collection db.yourCollectionName.find({}).forEach(function(doc) { if (typeof doc.yourFieldName === 'string') { doc.yourFieldName = ObjectId(doc.yourFieldName); // Convert to ObjectId type db.yourCollectionName.save(doc); } });