What a waste of a day! Built this beautiful csv import system, only to find out the the _id: object(string)
This sucks.
Best I got is having to rewrite a bunch of front end code to support these ID types… here ya go kids:
function convertToObjectId (id) {
// Check if id is in the format ObjectID("..."), extract the actual ID if needed
if (typeof id === 'string' && id.startsWith('ObjectID(')) {
id = id.match(/ObjectID\("(.*)"\)/)[1]; // Extract the ID part from the string
}
// Try to convert to Mongo ObjectID
try {
id = new Mongo.ObjectID(id);
} catch (e) {
console.error('Invalid ObjectID format:', e);
}
return id;
}
// Helpers
Template.contactModal.helpers({
'findContact'() {
window.tagsViewMode = 'full';
// Directly call the global helper convertToObjectId
const contactId = convertToObjectId(window.contactModalId);
// Perform the query
return Contacts.find({ _id: contactId });
},
Its the price to pay if you want massive bulk imports… In my case, 5 million.