// 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); } });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter